最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do you pass body data with an Amplify REST request? - Stack Overflow

programmeradmin1浏览0评论

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 0
Add a ment  | 

1 Answer 1

Reset to default 5

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);
  };
发布评论

评论列表(0)

  1. 暂无评论