Google端点GET请求URL参数[Python] [英] Google Endpoints GET request URL parameters [Python]
问题描述
我目前正在一个小项目中,我需要编写一个GET处理程序.我正在使用端点文档中提供的Echo示例.
I am currently working on a small project where I need to write a GET handler. I am working from the Echo example provided in the endpoints documentation.
我有我的资源容器:
GET_EMAIL_RESOURCE = endpoints.ResourceContainer(
message_types.VoidMessage,
i = messages.IntegerField(1, default = 1)
)
我有我的处理程序:
@endpoints.method(
GET_EMAIL_RESOURCE,
EchoResponse,
path='echo/getEmails/{i}',
http_method='GET',
name='echo_get_emails'
)
def echo_get_emails(self, request):
if (request.i == 1):
out = "default"
else:
out = "a"*request.i
return EchoResponse(content=out)
要访问此文件,我正在使用curl:
And to access this I am using curl:
curl --request GET --header "Content-Type: application/json" http://localhost:8080/_ah/api/echo/v1/echo/getEmails/3
现在这可以正常工作并返回您期望的结果,但是如果我需要将更多信息编码到我的URL中,例如:
Now this works fine and returns what you expect, but if I need to encode more information into my URL such as:
getEmails/value1=someValue&value2=someOtherValue
我不知道该怎么做,也无法在文档中找到解释或示例.
I cannot figure out how to do this and I am unable to find an explanation or example in the docs.
任何帮助将不胜感激.
我的代码现在看起来像这样:
My code now looks like so:
# main.py
GET_EMAIL_RESOURCE = endpoints.ResourceContainer(
message_types.VoidMessage,
i = messages.IntegerField(1, default = 1)
)
@endpoints.method(
GET_EMAIL_RESOURCE,
EchoResponse,
path='echo/getEmails',
http_method='GET',
name='echo_get_emails'
)
def echo_get_emails(self, request):
out = str(request.trailingDigits) + " : " +str(request.leadingDigits)
return EchoResponse(content = out)
,并且在我的openapi.json中,我具有以下路径:
and in my openapi.json under the path I have:
"/echo/v1/echo/getEmails" : {
"get": {
"operationId": "EchoApi_getEmails",
"parameters" : [
{
"name": "trailingDigits",
"in": "query",
"description": "Trailing digits",
"required": true,
"type": "string"
},
{
"name": "leadingDigits",
"in": "query",
"description": "Leading digits",
"required": true,
"type": "string"
}
]
}
}
但是我运行请求时curl --request GET --header"Content-Type:application/json" http://localhost:8080/_ah/api/echo/v1/echo/getEmails?trailingDigits = 2& leadingDigits = 2
Yet when I run the request curl --request GET --header "Content-Type: application/json" http://localhost:8080/_ah/api/echo/v1/echo/getEmails?trailingDigits=2&leadingDigits=2
我找回了TrailingDigits的内容,但是LeadingDigits的默认值
I get back the contents of trailingDigits but the default for leadingDigits
推荐答案
Google论坛中的此问题已为我解决了该问题:
This answer from Google forums fixed the issue for me:
您是否在curl命令中的URL周围使用引号?因为如果不,并且您处于bash外壳中(如果您使用curl),您实际上可能并没有发送& ends_at = THEEND部分,因为&是一个shell元字符,用于将命令放入背景.
Are you using quotes around the URL in your curl command? Because if not, and you're in a bash shell (which is pretty likely if you're using curl), you might not actually be sending the &ends_at=THEEND part, because & is a shell metacharacter to put the command in the background.
修改
链接到论坛中的答案 https://groups.google.com/forum/#!msg/google-cloud-endpoints/uzFcUS_uNz0/xyG2qVxbBQAJ
这篇关于Google端点GET请求URL参数[Python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!