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);
};
Below code works for me to send an SMS
var AWS = require('aws-sdk');
exports.handler = function (event, context, callback) {
console.log("Log-Event:"+event.message);
console.log("Log-Event:"+event.phoneNUmber);
// Create publish parameters
var params = {
Message: event.message, /* required */
PhoneNumber: event.phoneNumber,
MessageStructure: 'string'
};
var sns = new AWS.SNS();
// Create promise and SNS service object
var publishTextPromise = sns.publish(params).promise();
// Handle promise's fulfilled/rejected states
publishTextPromise.then(
function(data) {
console.log("MessageID is " + data.MessageId);
}).catch(
function(err) {
console.error(err, err.stack);
});
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.