Http Post请求被强制转换为Http Get请求

今天在开发一个服务的时候,发现用postman造的post请求,到了服务端变成了get请求。觉得很奇怪。于是打开wireshark抓包。

发现通信流程如下

1、post请求发起之后,服务端返回了302
在这里插入图片描述

2、客户端收到302之后,通过https向服务端发起了新的请求。 (wireshark抓包filter配置为:port 443 and host yourhost.com)
在这里插入图片描述

于是怀疑是客户端在处理302时,自动重新发起了get请求。

找了一下果然如此。

https://stackoverflow.com/questions/17605915/what-is-the-correct-behavior-expected-of-an-http-post-302-redirect-to-get

Note: RFC 1945 and RFC 2068 specify that the client is not allowed to change the method on the redirected request. However, most existing user agent implementations treat 302 as if it were a 303 response, performing a GET on the Location field-value regardless of the original request method. The status codes 303 and 307 have been added for servers that wish to make unambiguously clear which kind of reaction is expected of the client.

大多数http客户端(包括浏览器)会将引起302的原有的post请求转化为get请求发出去(其实这是将302当做303来处理)。

于是,将http换成https,问题解决。

至于为啥会将302当做303来处理,据说这是很多老服务器的期望行为。所以一切都是为了兼容。
————————————————
版权声明:本文为CSDN博主「tong_master」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/tongtest/article/details/103517257

你可能感兴趣的:(坑吧,http,网络协议,网络,经验分享)