The data bound to the POST request is gone while being redirected to GET with CURL

I wrote the following command to send a POST with JSON data to the server. The server should redirect my request and send a GET with the same data:

curl -L -i -XPOST \ -d 'id=105' \ -d 'json={"orderBy":0,"maxResults":50}' http://mysite.com/ctlClient/ 

I get the answer:

 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 the 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 not receiving my data added to the message. It seems that during the redirect, my data was somehow fired. It works from the Android client, so its problem is not on the server side.

What do I need to do to pass POST data to a GET request?

Thank you very much,

[EDIT]

@nif suggest updating CURL , I did, to 7.28.0.

Still having the same problems

[INFO]

The first time I will 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 redirecting, 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 correctly get 2 parameters: id and json

+6
source share
2 answers

From curl manpage :

When curl follows the redirect and the request is not a simple GET (for example, POST or PUT), it will execute the next request with GET if the HTTP response was 301, 302 or 303. If the response code was any other 3xx code, curl will resubmit the next request using the same unmodified method.

Edit

I did some research and found out that this could be a problem with your version of curl. A newer version will mark the -XPOST option and there will be POST in the redirected location. But older versions had their own version 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 to GET requests for 301 redirects. Non-RFC behavior is ubiquitous in web browsers, so curl does the default conversion to maintain consistency. However, the server may require POST to remain POST after the redirect. This option only makes sense when using -L, --location (Added in 7.17.1)

- post302 Tells curl to respect RFC 2616 / 10.3.2 and not convert POST requests to GET requests for redirects 302. Non-RFC behavior is ubiquitous in web browsers, so curl does the default conversion to maintain consistency. However, the server may require POST to remain POST after the redirect. This option only makes sense when using -L, --location (Added in 7.19.1)

Literature:

+6
source

I need to add -b to my script in order to enable cookies. CURL does not use them by default, and this problem caused a change in the session identifier. Therefore, data is not transmitted.

 curl -b -L -i -X POST \ -d 'id=105' \ -d 'json={"orderBy":0,"maxResults":50}' http://mysite.com/ctlClient/ 

Now it works

0
source

Source: https://habr.com/ru/post/948713/


All Articles