我正在使用angular将文件POST到Amazon S3存储桶。为了让存储桶接受请求,我尝试向其添加Upgrade-Insecure-Requests标头,当我这样做时,请求不再有正文。
此代码正确发送请求正文:
$http({
method: "POST",
url: responseData.url,
data: postData,
headers: {"Content-Type": undefined},
transformRequest: angular.identity
})
虽然此代码导致空请求正文:
$http({
method: "POST",
url: responseData.url,
data: postData,
headers: {"Content-Type": undefined,
"Upgrade-Insecure-Requests": "1"},
transformRequest: angular.identity
})
导致这种情况的原因是什么?
编辑:我发现没有请求正文的原因是因为正在向S3发送预检OPTIONS请求。我在桶中添加了一个允许OPTIONS和POST方法的CORS策略,所以现在OPTIONS请求正确返回。
然而,在收到OPTIONS响应后,即使POST符合要求,也不会发出POST。
这些是OPTIONS的请求标题:
OPTIONS / HTTP/1.1
Host: tesssssst-aws-cms.s3.amazonaws.com
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: https://s3.amazonaws.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36
Access-Control-Request-Headers: cache-control, upgrade-insecure-requests
Accept: */*
Referer: https://s3.amazonaws.com/tesssssst-aws-cms/HTML/upload.html
Accept-Encoding: gzip, deflate, sdch, br
Accept-Language: en-GB,en;q=0.8,en-US;q=0.6,ms;q=0.4,th;q=0.2
这些是OPTIONS的响应标题:
HTTP/1.1 200 OK
x-amz-id-2: SQaOyODgprPIRe8uq3YEZBDXzVcUjyYc2GpLsZwmIwdII+FVIgOGQXokIzXTsBcIaOITgwrk8Zw=
x-amz-request-id: 07EE6477F2B3273C
Date: Thu, 08 Sep 2016 07:12:58 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, GET, POST
Access-Control-Allow-Headers: cache-control, upgrade-insecure-requests
Access-Control-Max-Age: 3000
Vary: Origin, Access-Control-Request-Headers, Access-Control-Request-Method
Content-Length: 0
Server: AmazonS3
答案 0 :(得分:0)
“升级 - 不安全请求”:“1” 导致您的帖子请求作为选项发送我面临同样的问题所以我删除上面的行,它的工作原理。 之后我面临新的错误CrossDomain问题但是,当我用fiddler检查一切都很好。 那就是浏览器(Chrome)问题。