如何使用 Amplify REST 请求传递body数据?



我正在使用 AWS Amplify REST API 向 React Native 中的 lambda 函数发出 get 请求。API/Lambda 函数由 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);
};

除非我从此请求中删除正文,否则它只会返回没有其他详细信息的Error: Network Error。如果我删除身体对象,我似乎能够很好地获得queryStringParameters

如果我这样做,请求可以正常进行,没有错误

const myInit = JSON.stringify({
body: {
name: "bob",
},
});

但是 lambda 中event(event.body( 中的body始终为空。如果body也更改为data,则结果相同。我的第二个想法是,也许我只能通过POST请求传递身体数据,但是文档似乎表明您可以使用GET请求,因为它记录了如何访问所述身体数据......

λ函数

exports.handler = async(event) => {
const response = {
statusCode: 200,
body: JSON.stringify(event),
};
return response;
};

如何正确传递身体数据?

Amplify SDK 不会在API.get()调用中发送正文。您的第一个示例看起来不错,但您需要改用API.post()(或放置(。

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);
};

最新更新