Search code examples
google-app-enginegoogle-app-engine-pythongoogle-cloud-tasks

Cloud Tasks App Engine target with relative URI throws Exception 400 : HttpRequest.url is required


I am trying to create a task using Google Cloud Tasks using the Python client google-cloud-tasks==2.1.0 but I am getting an exception that HttpRequest.url is required. I am setting relative url which is a URL handling the task in my app.

The queue exists and has been created using:

gcloud task create queue notifications

The code:

client = tasks_v2.CloudTasksClient()
parent = client.queue_path(project, location, queue)
task = {                                                                
  'app_engine_http_request': {                                        
    'http_method': tasks_v2.HttpMethod.POST,                        
    'relative_uri': notification_url,
    'body': payload.encode('utf-8')                       
  },                                                                  
  'http_request': {                                                   
    'headers': {"Content-type": "application/json"}                 
  }                                                                   
}
response = client.create_task(parent=parent, task=task)       

The exact error that I receive is:

google.api_core.exceptions.InvalidArgument: 400 HttpRequest.url is required

I am trying to create task in my App Engine Standard environment.


Solution

  • @Donald was right, but i think there is a typo in the google docs he linked. I set my headers within app_engine_http_request, not http_request.

    I don't think you can provide both app_engine_http_request and http_request, you can only do one. So like this:

         client = tasks_v2.CloudTasksClient()
         parent = client.queue_path(project, location, queue)
         task = {                                                                
             'app_engine_http_request': {                                        
                 'http_method': tasks_v2.HttpMethod.POST,                        
                 'relative_uri': notification_url,
                 'headers': {
                     'Content-Type': 'application/json'
                 },
                 'body': payload.encode('utf-8')                       
             }                                                                 
         }
         response = client.create_task(parent=parent, task=task)
    

    https://googleapis.dev/python/cloudtasks/latest/tasks_v2/types.html#google.cloud.tasks_v2.types.AppEngineHttpRequest.headers