简体   繁体   English

具有相对 URI 的 Cloud Tasks App Engine 目标引发异常 400:需要 HttpRequest.url

[英]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.我正在尝试使用 Python 客户端google-cloud-tasks==2.1.0使用 Google Cloud Tasks 创建任务,但我收到一个异常,即需要 HttpRequest.url。 I am setting relative url which is a URL handling the task in my app.我正在设置相对 url 这是一个 URL 在我的应用程序中处理任务。

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.我正在尝试在我的 App Engine 标准环境中创建任务。

@Donald was right, but i think there is a typo in the google docs he linked. @Donald 是对的,但我认为他链接的谷歌文档中有错字。 I set my headers within app_engine_http_request , not http_request .我在app_engine_http_request中设置了我的标题,而不是http_request

I don't think you can provide both app_engine_http_request and http_request , you can only do one.我不认为你可以同时提供app_engine_http_requesthttp_request ,你只能做一个。 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 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.您的任务有两个目标,即 App Engine 和 HTTP。 On HTTP, a URL is required as specified in creating HTTP target tasks .在 HTTP 上,需要 URL,如创建 HTTP 目标任务中指定的那样。

The URL must start with 'http://' or 'https://'. URL 必须以“http://”或“https://”开头。 To fix the problem, update your http_request :要解决此问题,请更新您的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:或者,在声明您的任务后,删除http_request并像这样指定您的 header:

task["http_request"]["headers"] = {"Content-type": "application/json"} 任务[“http_request”][“headers”] = {“内容类型”:“应用程序/json”}

EDIT : When specifying App Engine headers, it is also possible to write it down this way:编辑:指定 App Engine 标头时,也可以这样写:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM