问题
The docs give the following as a mock example:
functions:
hello:
handler: handler.hello
events:
- http:
path: hello
cors: true
method: get
integration: mock
request:
template:
application/json: '{"statusCode": 200}'
response:
template: $input.path('$')
statusCodes:
201:
pattern: ''
This does create a mock response...except it's empty.
How can I actually return data here? I've tried adding application/json: {...}
to template
, but that doesn't work, I've tried adding a body under statusCodes
but no luck there either.
There doesn't seem to be any documentation on this...how can I return an actual body?
回答1:
You can do this by setting the value of response.template
. However, this isn't done using an application/json
key like request
is, you just set template
directly.
Return a string foo
response:
template: "foo"
statusCodes:
201:
pattern: ''
Return JSON
response:
template: ${file(foo.txt)}
statusCodes:
201:
pattern: ''
# Where foo.txt contains regular JSON
{
"foo":"bar"
}
回答2:
This is what I do to return mock response data...
functions:
helloworld:
handler: api/handler.mock
events:
- http:
path: ''
method: get
integration: mock
request:
template:
application/json: '{"statusCode": 200}'
response:
template: '{"code": 200,"message": "Helloworld!"}'
statusCodes:
200:
body: '{"code": 200,"message": "Helloworld!"}'
来源:https://stackoverflow.com/questions/59778964/mock-response-data-with-serverless-framework