Python Google云端点/如何获取json正文请求?
我试图在POST请求发送到端点时获取json正文 我这样定义了我的方法和类:Python Google云端点/如何获取json正文请求?,python,api,curl,google-cloud-platform,google-cloud-endpoints,Python,Api,Curl,Google Cloud Platform,Google Cloud Endpoints,我试图在POST请求发送到端点时获取json正文 我这样定义了我的方法和类: class UserRequest(messages.Message): f1 = messages.IntegerField(1) f2 = messages.StringField(2) class UserResponse(messages.Message): content = messages.StringField(1) USER_RESOURCE = endpoints.Res
class UserRequest(messages.Message):
f1 = messages.IntegerField(1)
f2 = messages.StringField(2)
class UserResponse(messages.Message):
content = messages.StringField(1)
USER_RESOURCE = endpoints.ResourceContainer(
UserRequest)
@endpoints.api(name='echo', version='v1')
class EchoApi(remote.Service):
@endpoints.method(
USER_RESOURCE,
UserResponse,
path='echo',
http_method='POST',
name='echo')
def echo(self, request):
data = {x.name: getattr(request, x.name) for x in request.all_fields()}
return UserResponse(content=str(data))
当我将卷曲柱发送到端点时,没有包含主体:
curl -x POST http://localhost:8080/_ah/api/echo/v1/echo?f1=asdf&f2=qwert
我得到了带有request.f1和request.f2的f1和f2
但当我用身体打卷发电话时:
curl -H "Content-Type: application/json" -X POST -d '{"f1": "asdf", "f2":"qwert"}' http://localhost:8080/_ah/api/echo/v1/echo
请求对象不包含f1和f2值,它们保留在中
我想知道是否可以作为一个经典请求直接获取json正文。您不需要使用
ResourceContainer
。只需将请求设置为UserRequest,就可以修复它。