curl:重定向302:不将方法从POST更改为GET [英] curl: redirect 302: Not changing the method from POST to GET

查看:71
本文介绍了curl:重定向302:不将方法从POST更改为GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 url(/add_item),我可以使用 POST 方法向其发送数据.完成任务后,它将重定向到 url2(/show_items)

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)

我使用curl命令(POST方法以及--location用于自动重定向).以下是命令

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"
}'

输出

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>

在这里我注意到

* 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

由于其<HTTP/1.1 302 Found curl应该使用GET方法而不是POST.但其使用POST.如何停止这种行为.

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

它似乎正在尝试使用GET

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

但是稍后又改回POST

But later changes back to POST

如何确保它使用GET

How to ensure it uses GET

推荐答案

注意:不必要使用-X或--request,已经推断出POST.

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

上述原因===删除-request POST 以及"--data-raw"应该足以触发POST请求,而不会产生不希望的影响.

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

答案来自 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"
}'

这篇关于curl:重定向302:不将方法从POST更改为GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆