我正在尝试使用 fetch() 通过来自 React 的 API 调用连接到 AWS 服务。这是调用代码(**** 是经过审查的内容):
fetch("https://runtime.lex.us-east-1.amazonaws.com/bot/****/alias/****/user/****/text", {
method: "POST",
body: "inputText: hi",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Host": "runtime.lex.us-east-1.amazonaws.com",
"X-Amz-Date": "20181229T190205Z",
"Authorization": "AWS4-HMAC-SHA256 Credential=*****/20181229/us-east-1/lex/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date, Signature=*****************************************",
"Content-Length": "27"
}
})
具有相同标头的相同 API 调用适用于 chrome 中的 Postman 应用程序并返回有效响应。
但是,当使用 fetch 调用此 API 时,出现错误:
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."
尽管我使用与来自邮递员的 API 调用相同的标题内容,但这种情况发生了,这应该可以持续 5 分钟,而无需根据新日期重新计算签名。
我注意到的一件事是标题“主机”没有出现在开发人员工具中的请求上 - > chrome上的网络选项卡。
它可能与问题有关?,我不知道为什么它没有出现在那里,因为我清楚地设置了这个标题。以下是我看到的请求标头(顶部有消息说:“显示临时标头”):
Authorization: AWS4-HMAC-SHA256 Credential=***************/20181229/us-east-1/lex/aws4_request, SignedHeaders=content-length;content-type;host;x-amz-date, Signature=************************
Content-Type: application/x-www-form-urlencoded
Origin: http://localhost:3000
Referer: http://localhost:3000/
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
X-Amz-Date: 20181229T190205Z