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

Viewed 887

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.

2 Answers

@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

Your task have two targets which is App Engine and HTTP. On HTTP, a URL is required as specified in creating HTTP target tasks.

The URL must start with 'http://' or 'https://'. To fix the problem, update your http_request:

'http_request': {                                                   
  'headers': {"Content-type": "application/json"},
  'url': "https://[SERVICE-URL]" + notification_url              
}

Or, remove http_request and specify your header like this just after declaring your task:

task["http_request"]["headers"] = {"Content-type": "application/json"}

EDIT: When specifying App Engine headers, it is also possible to write it down this way:

task["app_engine_http_request"]["headers"] = {"Content-type": "application/json"}
Related