当POST从http重定向到https时,Curl将丢失正文

当POST从http重定向到https时,Curl将丢失正文,post,redirect,curl,Post,Redirect,Curl,我正在尝试使用curl7.23.1将一些数据发布到我的服务器(它运行nginx服务器)。当使用http://查询此nginx时,如果可以,它将重定向到https://(如果它有密钥和所有mambo巨型机) 看起来curl在做什么。。。古怪的出于兼容性原因,我的脚本(运行curl的东西)正在将其请求发送到http://,但当它到达具有可用https://的服务器时(因此被重定向),POST请求中的主体将消失 我正在尝试使用option上传一堆文本。基本上,我运行一个脚本,将其输出转储到文件/tmp

我正在尝试使用curl7.23.1将一些数据发布到我的服务器(它运行
nginx
服务器)。当使用
http://
查询此
nginx
时,如果可以,它将重定向到
https://
(如果它有密钥和所有mambo巨型机)

看起来curl在做什么。。。古怪的出于兼容性原因,我的脚本(运行
curl
的东西)正在将其请求发送到
http://
,但当它到达具有可用
https://
的服务器时(因此被重定向),
POST
请求中的主体将消失

我正在尝试使用option上传一堆文本。基本上,我运行一个脚本,将其输出转储到文件
/tmp/checkin/cmd_result
,并将其作为请求的“数据”属性上载:

curl --max-time 30 --silent --location-trusted \
     --insecure --write-out "%{http_code}" --request POST \
     --data-urlencode "data@/tmp/checkin/cmd_result" \
     "http:/myserver/command_reply?command_id=524729ce93bc63292c1c9831" \
     --trace-ascii /tmp/command_response_trace.log \
     --output /tmp/checkin/cmd_res_resp --connect-timeout 10
但是,重定向似乎剥夺了身体。如果我直接将数据发送到
https://
,一切都会像一个符咒一样工作。将
-L
--location
)或
--location trusted
添加到curl指令似乎无法解决问题

有人经历过同样的问题吗?任何帮助都将不胜感激

附言:

这是跟踪
curl
输出(我已经清理了一点):

=>发送头,300字节(0x12c)
0000:发布/命令回复?&命令id=52
0040:4729ce93bc63292c1c9831 HTTP/1.1
0061:用户代理:curl/7.23.1(mips openwrt linux gnu)libcurl/7.23.1
00a1:OpenSSL/1.0.1c zlib/1.2.7
00e4:内容长度:356
00f9:内容类型:应用程序/x-www-form-urlencoded
012a:
=>发送数据,356字节(0x164)
0000:data=PING%20google.com%0A

看起来您的nginx执行了从http到https的302重定向。基于RFC2616标准,您应该在nginx配置中使用301或307重定向。请参阅下面链接中的更多信息

请注意,一些用户代理(chrome、firefox、IE)更进一步,将302重定向视为GET请求,即使原始请求可能是POST

参考:

注意:RFC 1945和RFC 2068规定不允许使用客户端 更改重定向请求的方法。然而,大多数 现有的用户代理实现将302视为303 响应,对位置字段值执行GET,不管 原始请求方法的。状态代码303和307已被删除 已添加用于希望明确说明 客户会有什么样的反应

更新

301也可能不起作用。在卷曲手册中可以找到这个。看起来curl也违反了RFC2616。所以你可能想先试试307

当curl遵循重定向并且请求不是简单的GET时 (例如POST或PUT),它将使用 如果HTTP响应为301、302或303,则返回GET。如果回答是 代码是任何其他3xx代码,curl将重新发送以下内容 使用相同的未修改方法进行请求


有趣的。。。我会告诉管理服务器的人,然后告诉你。谢谢你的回复!我的理解是,curl还应该在收到302响应后发送GET请求。能否在命令中删除此选项
--request POST
,并查看curl是否将第二个请求作为GET请求发送?因为您有
--data urlencode
选项,所以curl应该将第一个请求作为POST发送。我猜,
--request POST
可能会迫使curl将两个请求都作为POST发送。是的,如果您好奇的话,这是Nginx服务器处理所有这些的问题。我还不知道所有的细节,但我知道这是一个路由问题。301不起作用,它将POST请求重定向为GET。
=> Send header, 300 bytes (0x12c)
0000: POST /command_reply?&command_id=52
0040: 4729ce93bc63292c1c9831 HTTP/1.1
0061: User-Agent: curl/7.23.1 (mips-openwrt-linux-gnu) libcurl/7.23.1 
00a1: OpenSSL/1.0.1c zlib/1.2.7
00e4: Content-Length: 356
00f9: Content-Type: application/x-www-form-urlencoded
012a: 
=> Send data, 356 bytes (0x164)
0000: data=PING%20google.coms%0A
<= Recv header, 32 bytes (0x20)
0000: HTTP/1.1 302 Moved Temporarily
<= Recv header, 22 bytes (0x16)
0000: Server: nginx/1.1.19
<= Recv header, 37 bytes (0x25)
0000: Date: Sat, 28 Sep 2013 19:11:27 GMT
<= Recv header, 25 bytes (0x19)
0000: Content-Type: text/html
<= Recv header, 21 bytes (0x15)
0000: Content-Length: 161
<= Recv header, 24 bytes (0x18)
0000: Connection: keep-alive
<= Recv header, 120 bytes (0x78)
0000: Location: https://myserver/sensor/command_reply?command_id
0040: =524729ce93bc63292c1c9831
<= Recv header, 2 bytes (0x2)
0000: 
<= Recv data, 161 bytes (0xa1)
0000: <html>
0008: <head><title>302 Found</title></head>
002f: <body bgcolor="white">
0047: <center><h1>302 Found</h1></center>
006c: <hr><center>nginx/1.1.19</center>
008f: </body>
0098: </html>
== Info: SSLv3, TLS handshake, Client hello (1):
=> Send SSL data, 189 bytes (0xbd)
0000: .....
== Info: SSLv3, TLS handshake, Server hello (2):
<= Recv SSL data, 90 bytes (0x5a)
0000: .....
== Info: SSLv3, TLS handshake, CERT (11):
<= Recv SSL data, 3227 bytes (0xc9b)
0000: .....?.
== Info: SSLv3, TLS handshake, Server key exchange (12):
<= Recv SSL data, 525 bytes (0x20d)
0000: .^~...
== Info: SSLv3, TLS handshake, Server finished (14):
<= Recv SSL data, 4 bytes (0x4)
0000: ....
== Info: SSLv3, TLS handshake, Client key exchange (16):
=> Send SSL data, 134 bytes (0x86)
0000: ...'
== Info: SSLv3, TLS change cipher, Client hello (1):
=> Send SSL data, 1 bytes (0x1)
0000: .
== Info: SSLv3, TLS handshake, Finished (20):
=> Send SSL data, 16 bytes (0x10)
0000: ....
== Info: SSLv3, TLS change cipher, Client hello (1):
<= Recv SSL data, 1 bytes (0x1)
0000: .
== Info: SSLv3, TLS handshake, Finished (20):
<= Recv SSL data, 16 bytes (0x10)
0000: ...
=> Send header, 230 bytes (0xe6)
0000: POST /sensor/command_reply?command_id=52
0040: 4729ce93bc63292c1c9831 HTTP/1.1
0061: User-Agent: curl/7.23.1 (mips-openwrt-linux-gnu) libcurl/7.23.1 
00a1: OpenSSL/1.0.1c zlib/1.2.7
00bc: Host: myserver
00d7: Accept: */*
00e4: 
<= Recv header, 17 bytes (0x11)
0000: HTTP/1.1 200 OK
<= Recv header, 22 bytes (0x16)
0000: Server: nginx/1.1.19
<= Recv header, 37 bytes (0x25)
0000: Date: Sat, 28 Sep 2013 19:11:27 GMT
<= Recv header, 47 bytes (0x2f)
0000: Content-Type: application/json; charset=UTF-8
<= Recv header, 20 bytes (0x14)
0000: Content-Length: 22
<= Recv header, 24 bytes (0x18)
0000: Connection: keep-alive
<= Recv header, 2 bytes (0x2)
0000: 
<= Recv data, 22 bytes (0x16)
0000: {"r": 200, "data": {}}
== Info: SSLv3, TLS alert, Client hello (1):
=> Send SSL data, 2 bytes (0x2)
0000: ..