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

Mak*_*poc 11 redirect curl http

根据文档和关于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
Run Code Online (Sandbox Code Playgroud)

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

curl的手册页说:

当curl遵循重定向并且请求不是普通的GET(例如POST或PUT)时,如果HTTP响应是301,302或303,它将使用GET执行以下请求.如果响应代码是任何其他3xx代码,curl将使用相同的未修改方法重新发送以下请求.

您可以告诉curl在30x响应之后不使用专用选项将非GET请求方法更改为GET: - post301, - post302和--post303.

不幸的是,这不是我所看到的,并且没有--get30x的选项.

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

我用curl/7.29.0以及curl/7.50.3测试了它.

Dan*_*erg 15

问题:您使用-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.
Run Code Online (Sandbox Code Playgroud)

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

更多:解释和咆哮在我的博客发布不必要的curl -X使用.

  • 谢谢,它有效。提供一个空的正文而不是仅仅说 -X POST 似乎有点违反直觉,但确实我错过了手册页的那部分。 (2认同)
  • 所以,如果我有`PUT`或`PATCH`而不是`POST`,那我就走运了吗? (2认同)