curl: redirect 302: Not changing the method from POST to GET

拈花ヽ惹草 提交于 2021-01-28 05:10:07

问题


I have a url(/add_item) to which i send data using a POST method. After finishing the task it will redirect to url2 (/show_items)

I using the curl command (POST method and also --location is used to auto redirect). The following is the command

$ curl --verbose --location --request POST 'http://127.0.0.1:8000/add_item' \
--header 'Content-Type: application/json' \
--data-raw '{
"item" : "new"
}'

The output

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> POST /add_item HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json
> Content-Length: 18
> 
* upload completely sent off: 18 out of 18 bytes
< HTTP/1.1 302 Found
< Date: Wed, 25 Mar 2020 08:02:37 GMT
< Server: WSGIServer/0.2 CPython/3.7.3
< Content-Type: text/html; charset=utf-8
< Location: /show_items
< Content-Length: 0
< X-Content-Type-Options: nosniff
< 
* Connection #0 to host 127.0.0.1 left intact
* Issue another request to this URL: 'http://127.0.0.1:8000/show_items'
* Switch from POST to GET
* Found bundle for host 127.0.0.1: 0x56410d38f180 [can pipeline]
* Could pipeline, but not asked to!
* Re-using existing connection! (#0) with host 127.0.0.1
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> POST /show_items HTTP/1.1
> Host: 127.0.0.1:8000
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Type: application/json
> 
< HTTP/1.1 500 Internal Server Error
< Date: Wed, 25 Mar 2020 08:02:37 GMT
< Server: WSGIServer/0.2 CPython/3.7.3
< Content-Type: text/html
< Content-Length: 99523
< Vary: Cookie
< X-Content-Type-Options: nosniff
< 
<!DOCTYPE html>
<html lang="en">
<head>
<body>
....
</body>
</head>
</html>

Here I notice that

* Re-using existing connection! (#0) with host 127.0.0.1
* Connected to 127.0.0.1 (127.0.0.1) port 8000 (#0)
> POST /show_items HTTP/1.1

Since its < HTTP/1.1 302 Found curl should use GET method instead of POST. But its using POST. How to stop this behaviour.

It looks its trying to use GET

* Issue another request to this URL: 'http://127.0.0.1:8000/show_items'
* Switch from POST to GET

But later changes back to POST

How to ensure it uses GET


回答1:


Note: Unnecessary use of -X or --request, POST is already inferred.

The above reason === remove --request POST also "--data-raw" should be enough to trigger POST request without that undesired effect.

answer is taken from https://github.com/curl/curl/issues/5145#issuecomment-603899140

So the below command works well:

$ curl --verbose --location 'http://127.0.0.1:8000/login_register_password/api/user_login_via_otp_form_otp' \     
--header 'Content-Type: application/json' \
--data-raw '{
"item" : "new"
}'


来源:https://stackoverflow.com/questions/60845042/curl-redirect-302-not-changing-the-method-from-post-to-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!