Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/65891363/cloud-tasks-app-engine-target-with-relative-uri-throws-exception-400-httpreque

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...