Page updated Jan 16, 2024

Update data

POST data

Posts data to the API endpoint:

1const apiName = 'MyApiName'; // replace this with your api name.
2const path = '/path'; //replace this with the path you have configured on your API
3const myInit = {
4 body: {}, // replace this with attributes you need
5 headers: {} // OPTIONAL
6};
7
8API.post(apiName, path, myInit)
9 .then((response) => {
10 // Add your code here
11 })
12 .catch((error) => {
13 console.log(error.response);
14 });

Example with async/await

1async function postData() {
2 const apiName = 'MyApiName';
3 const path = '/path';
4 const myInit = {
5 body: {}, // replace this with attributes you need
6 headers: {} // OPTIONAL
7 };
8
9 return await API.post(apiName, path, myInit);
10}
11
12postData();

PUT data

When used together with a REST API, put() method can be used to create or update records. It updates the record if a matching record is found. Otherwise, a new record is created.

1const apiName = 'MyApiName'; // replace this with your api name.
2const path = '/path'; // replace this with the path you have configured on your API
3const myInit = {
4 body: {}, // replace this with attributes you need
5 headers: {} // OPTIONAL
6};
7
8API.put(apiName, path, myInit)
9 .then((response) => {
10 // Add your code here
11 })
12 .catch((error) => {
13 console.log(error.response);
14 });

Example with async/await:

1async function putData() {
2 const apiName = 'MyApiName';
3 const path = '/path';
4 const myInit = {
5 body: {}, // replace this with attributes you need
6 headers: {} // OPTIONAL
7 };
8
9 return await API.put(apiName, path, myInit);
10}
11
12putData();

Access body in the Lambda function

1// using a basic lambda handler
2exports.handler = (event, context) => {
3 console.log('body: ', event.body);
4};
5
6// using serverless express
7app.put('/myendpoint', function (req, res) {
8 console.log('body: ', req.body);
9});

Update a record:

1const params = {
2 body: {
3 itemId: '12345',
4 itemDesc: ' update description'
5 }
6};
7
8const apiResponse = await API.put('MyTableCRUD', '/manage-items', params);

Access body in Lambda proxy function

1// using a basic lambda handler
2exports.handler = (event, context) => {
3 console.log('body: ', event.body);
4};
5
6// using serverless express
7app.post('/myendpoint', function (req, res) {
8 console.log('body: ', req.body);
9});