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

According to the documentation and some similar questions on SO curl should follow a redirect using GET method, unless --post30x is specified as a parameter. However that's the result of my testing

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

I would expect the second request to actually be using GET instead of POST.

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.

Unfortunatelly that's not what I'm seeing and there is no option for --get30x.

So my question is - how to make curl follow a redirect response (301/302/303) with a GET request to the Location as it is written in the documentation?

I've tested it with curl/7.29.0 as well as curl/7.50.3.

解决方案

Problem: You are telling curl to do that with your use of -X POST. As the man page section for -X explains this:

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.

Fix: Remove the -X POST from your command line. Use -d "" instead to send an empty post that will adjust accordingly to the proper method after redirect.

More: Explanation and rant in my blog post unnecessary use of curl -X.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值