Data attached to POST request is gone during redirection to GET with CURL
curl, linux, redirect
Solution
From curl's manpage:
When curl follows a redirect and the request is not a plain GET (for example POST or PUT), it will do the following request with a GET if the HTTP response was 301, 302, or 303. If the response code was any other 3xx code, curl will re-send the following request using the same unmodified method.
Edit
I did some research and found out, that it might be a problem with your curl version. Newer version will honour the `-XPOST` option and will `POST` to the redirected location as well. But older versions had an own option for this, i.e. `--post301` and `--post302`. According to their manpage:
--post301 Tells curl to respect RFC 2616/10.3.2 and not convert POST requests into GET requests when following a 301 redirection. The non-RFC behaviour is ubiquitous in web browsers, so curl does the conversion by default to maintain consistency. However, a server may require a POST to remain a POST after such a redirection. This option is meaningful only when using -L, --location (Added in 7.17.1)
--post302 Tells curl to respect RFC 2616/10.3.2 and not convert POST requests into GET requests when following a 302 redirection. The non-RFC behaviour is ubiquitous in web browsers, so curl does the conversion by default to maintain consistency. However, a server may require a POST to remain a POST after such a redirection. This option is meaningful only when using -L, --location (Added in 7.19.1)
References:
- Following redirects with curl
- HTTP RFC
Problem
I wrote followed command to send POST with JSON data to server. The server must redirect my request and send GET with the same data: ``` curl -L -i -XPOST \ -d 'id=105' \ -d 'json={"orderBy":0,"maxResults":50}' http://mysite.com/ctlClient/ ``` I get response: ``` HTTP/1.1 302 Found Date: Thu, 04 Jul 2013 13:12:08 GMT Server: Apache X-Powered-By: PHP/5.3.19 Set-Cookie: PHPSESSID=1hn0g8d7gtfl4nghjvab63btmk2; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache location: http://mysite.com/fwf/online/ Content-Length: 0 Connection: close Content-Type: text/html HTTP/1.1 200 OK Date: Thu, 04 Jul 2013 13:12:08 GMT Server: Apache X-Powered-By: PHP/5.3.19 Set-Cookie: PHPSESSID=16akc7kdcoet71ipjflk9o9cnm5; path=/ Expires: Thu, 19 Nov 1981 08:52:00 GMT Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0 Pragma: no-cache Content-Length: 1 Connection: close Content-Type: text/html ``` From access-log I see: ``` "POST /ctlClient/ HTTP/1.1" 302 - "-" "Apache-HttpClient/4.1 (java 1.5)" "GET /fwf/online/ HTTP/1.1" 200 1 "-" "Apache-HttpClient/4.1 (java 1.5)" ``` So far so good, The problem is that `GET` doesn't receives my data added to post. Sounds like during redirect my data dismissed somehow. From Android client it works, therefore its not Server side problem. What I need to do to pass POST data to GET request? Thank you very much, [EDIT] @nif offerd to upgrade `CURL`, i did , to 7.28.0. Still get the same problem [INFO] 1st time i go to `http://mysite.com/ctlClient/index.php` where: ``` case 105: // id=105 session_unset(); session_start(); foreach($_POST as $key => $value){$_SESSION[$key] = $value;} ctlGotoSameDomain("/fwf/online/"); // <- aka redirect return true; ``` after redirect i go to `/fwf/online/index.php` and there my request is empty: ``` public function __construct() { $this->json = isset($_SESSION['json']) ? $_SESSION['json'] : null; msqLogFile("fwf/post", Array('post' => 'Request: '.$this->json)); } ``` `http://mysite.com/ctlClient/index.php` get 2 parameters properly: `id` and `json`