【问题标题】:Error: Request failed with status code 403 (GET)错误:请求失败,状态码为 403 (GET)
【发布时间】:2020-08-15 23:52:16
【问题描述】:

当我尝试从我的 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

【问题讨论】:

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


    【解决方案1】:

    简单来说,API网关不支持GET请求的请求体,所以需要使用路径参数。

    解决方案是将我的所有 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));
    

    【讨论】:

      猜你喜欢
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-21
      • 2021-09-15
      • 2021-04-10
      • 2017-11-15
      相关资源
      最近更新 更多