错误:请求失败,状态码为403(GET)

时间:2020-08-15 23:52:16

标签: aws-lambda aws-api-gateway aws-amplify

当我尝试从我的API调用GET /dev/get-comments端点时遇到错误。现在,我的API可以正常工作了,因为我的另一个GET /dev/get-posts端点工作正常,两者之间的唯一区别是第一个端点使用了请求正文。

API.get("holler-api", "/get-comments", {
  body: {postId: this.props.post.postId}
}).then(result => {
  if(result.Count > 0) {
    this.setState({
      comments: result.Items
    });
  }
})
.catch(err => console.log(err));
XHR GET https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/get-comments
[HTTP/2 403 Forbidden 23ms]

这是解释错误的请求响应,不是很有帮助!

SyntaxError: JSON.parse: bad control character in string literal at line 1 column 191 of the JSON data
{"message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.
The Canonical String for this request should have been
'GET
/dev/get-comments

1 个答案:

答案 0 :(得分:0)

简而言之,API网关不支持GET请求的请求主体,因此您需要使用路径参数。 enter image description here

解决方案是将我所有的GET lambda函数转换为使用路径参数,并这样调用API:

API.get("holler-api", `/get-comments/${this.props.post.postId}`)
.then(result => {
  if(result.Count > 0) {
    console.log(result.Items);
    this.setState({
      comments: result.Items
    });
  }
})
.catch(err => console.log(err));