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?
The 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);
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With