http - curl 在重定向后对所有请求使用 POST

标签 http redirect curl

根据文档和关于 SO 的一些类似问题,curl 应该遵循使用 GET 方法的重定向,除非将 --post30x 指定为参数。然而这是我测试的结果

curl -kvv -b /tmp/tmp.BEo6w3GKDq -c /tmp/tmp.BEo6w3GKDq -X POST -H "Accept: application/json" -L https://localhost/api/v1/resource

> POST /api/v1/resource HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost
> Cookie: JSESSIONIDSSO=AB59F2FD09D38EDBAACB726CF212EA2E; JSESSIONID=743FD68B520840094B6D283A81CF3CFA
> Accept: application/json
> 
< HTTP/1.1 302 Found
< Server: Apache-Coyote/1.1    
< Strict-Transport-Security: max-age=15768000; includeSubDomains
< Cache-control: no-cache, no-store
< Pragma: no-cache
< Location: https://testserver.int/api/v1/resource
< Content-Length: 0
< Date: Fri, 27 Jan 2017 08:41:05 GMT
<
> POST /api/v1/resource HTTP/1.1
> User-Agent: curl/7.29.0
> Host: testserver.int
> Cookie: JSESSIONID=1tcxpkul4qyqh1hycpf9insei9
> Accept: application/json

我希望第二个请求实际上使用 GET 而不是 POST。

curl's man page说:

When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmodified method.

You can tell curl to not change the non-GET request method to GET after a 30x response by using the dedicated options for that: --post301, --post302 and --post303.

不幸的是,这不是我所看到的,也没有 --get30x 选项。

所以我的问题是 - 如何使 curl 遵循重定向响应 (301/302/303) 并向文档中所写的位置发出 GET 请求?

我已经使用 curl/7.29.0 和 curl/7.50.3 对其进行了测试。

最佳答案

问题:您使用 -X POST 告诉 curl 这样做。正如 -X 的手册页部分解释的那样:

The method string you set with -X, --request will be used for all requests, which
if you for example use -L, --location may cause unintended side-effects when curl
doesn't change request method according to the HTTP 30x response codes - and
similar.

修复:从命令行中删除 -X POST。使用 -d ""而不是发送一个空帖子,该帖子将在重定向后相应地调整为正确的方法。

更多: 我的博文中的解释和咆哮 unnecessary use of curl -X .

关于http - curl 在重定向后对所有请求使用 POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41890172/

相关文章:

.htaccess - 301重定向与mod_rewrite

ubuntu - curl "-s"和 "-m"是什么意思

curl - curl的--upload-file是发布请求吗?

java - HTTP 请求 : ';' character in the request URL

java - 正确执行cURL请求

php - Htaccess - 验证是否 Cookie ISSET

java - 如何使用 java 将 curl 命令转换为 HTTP POST 请求

android - 在android中获取http响应的替代方法

java - 无法使用 HttpClient 获取 'location' header 作为响应

php - 显示重定向 In.. CountDown Timer PHP