I'm using the AWS amplify REST API to make a get request to my lambda function in React Native. The API/Lambda function was generated by the Amplify CLI.
const getData = async (loca) => {
const apiName = "api1232231321";
const path = "/mendpoint";
const myInit = {
body: {
name: "bob",
},
queryStringParameters: {
location: JSON.stringify(loca),
},
};
return API.get(apiName, path, myInit);
};
Unless I remove the body from this request, it just returns Error: Network Error
with no other details. I seem to be able to get the queryStringParameters
just fine though if I remove the body object.
If I do this, the request goes through fine without errors
const myInit = JSON.stringify({
body: {
name: "bob",
},
});
But the body
in the event
(event.body) in lambda is always null. Same result if change body
to data
as well. My second thought was that perhaps I can only pass body data with a POST
request however the docs seem to show that you can use a GET
request since it documents how to access said body data...
Lambda function
exports.handler = async(event) => {
const response = {
statusCode: 200,
body: JSON.stringify(event),
};
return response;
};
How do I correctly pass body data?
I'm using the AWS amplify REST API to make a get request to my lambda function in React Native. The API/Lambda function was generated by the Amplify CLI.
const getData = async (loca) => {
const apiName = "api1232231321";
const path = "/mendpoint";
const myInit = {
body: {
name: "bob",
},
queryStringParameters: {
location: JSON.stringify(loca),
},
};
return API.get(apiName, path, myInit);
};
Unless I remove the body from this request, it just returns Error: Network Error
with no other details. I seem to be able to get the queryStringParameters
just fine though if I remove the body object.
If I do this, the request goes through fine without errors
const myInit = JSON.stringify({
body: {
name: "bob",
},
});
But the body
in the event
(event.body) in lambda is always null. Same result if change body
to data
as well. My second thought was that perhaps I can only pass body data with a POST
request however the docs seem to show that you can use a GET
request since it documents how to access said body data...
Lambda function
exports.handler = async(event) => {
const response = {
statusCode: 200,
body: JSON.stringify(event),
};
return response;
};
How do I correctly pass body data?
Share Improve this question asked Jul 2, 2020 at 8:39 ProEvilzProEvilz 5,44511 gold badges45 silver badges80 bronze badges 01 Answer
Reset to default 5The Amplify SDK won't send a body on a API.get()
call. You're first example looks fine but you'll need to use API.post()
(or put) instead.
const getData = async (loca) => {
const apiName = "api1232231321";
const path = "/mendpoint";
const myInit = {
body: {
name: "bob",
},
queryStringParameters: {
location: JSON.stringify(loca),
},
};
return API.post(apiName, path, myInit);
};
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742328834a4423297.html
评论列表(0条)