【问题标题】:curl: redirect 302: Not changing the method from POST to GETcurl:重定向 302:不将方法从 POST 更改为 GET
【发布时间】:2020-07-05 18:35:22
【问题描述】:

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

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

$ 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

因为它的&lt; HTTP/1.1 302 Found curl 应该使用 GET 方法而不是 POST。但它使用POST。如何阻止这种行为。

看起来它正在尝试使用 GET

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

但后来又改回 POST

如何确保它使用 GET

【问题讨论】:

    标签: curl http-response-codes


    【解决方案1】:

    注意:不必要地使用 -X 或 --request,POST 已被推断。

    上述原因 === remove --request POST 以及“--data-raw”应该足以触发 POST 请求而不会产生不良影响。

    答案来自https://github.com/curl/curl/issues/5145#issuecomment-603899140

    所以下面的命令运行良好:

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

    【讨论】:

      猜你喜欢
      • 2012-09-14
      • 1970-01-01
      • 2016-02-21
      • 2016-09-15
      • 2011-11-02
      • 1970-01-01
      • 1970-01-01
      • 2019-02-01
      • 1970-01-01
      相关资源
      最近更新 更多