gpt4 book ai didi

json - AWS API 网关 - 调用 API 方法时出错 - 'Could not parse request body into json'

转载 作者:行者123 更新时间:2023-12-01 11:29:50 27 4
gpt4 key购买 nike

我有一个简单的 POST 方法连接到我的 AWS API Gateway 中的 lambda 函数.

当我执行测试(通过网关 API 控制台)时,一切正常,测试得到了我正在寻找的结果。很简单 - 发布一个 JSON,取回一个 JSON。

但是,在部署 API 并发送测试中使用的相同 JSON 之后(通过 http post),我得到了“无法将请求正文解析为 json”

有谁知道我可能做错了什么?注意:我不想使用模型,我只想传递 JSON。我相信当亚马逊写下“输入直通”之类的东西时它们意味着输入可以传递给 lambda 函数。

这是我的 AWS Gateway API 设置的图像:

方法请求: enter image description here

集成请求: enter image description here

方法响应: enter image description here

集成响应: enter image description here

最佳答案

我找到了解决方案。问题在于,在 POST 请求中,您需要将正文作为字符串而不是 JSON 对象发送,并且该字符串的格式需要正确

即'{“key1”:“val1”,“key2”:22,“key3”:15,“key4”:“val4”}'

像这样:

function post() {


$.ajax({
url: "https://myapi.us-east-1.amazonaws.com/myStage/myPath",
type: "POST",
data: '{"key1": "val1","key2": 22,"key3": 15,"key4": "val4"}',
mozSystem: true,
dataType: "text", //set to"json" usually
success: function (result) {
switch (result) {
case true:
processResponse(result);
break;
default:
resultDiv.html(result);
}
},
error: function (xhr, ajaxOptions, thrownError) {
alert('error ' + xhr.status + ' ' + thrownError);
alert(thrownError);
}
});


};

关于json - AWS API 网关 - 调用 API 方法时出错 - 'Could not parse request body into json',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33422540/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com