Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data attached to POST request is gone during redirection to GET with CURL

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

like image 894
Maxim Shoustin Avatar asked Jul 04 '13 13:07

Maxim Shoustin


People also ask

Does curl automatically follow redirects?

By default, Curl does not follow redirects and displays the content of the 300x page (if any). To follow redirects with Curl, you need to use the -L or --location command-line option.

How do I follow redirects with curl?

Tell curl to follow redirects In curl's tradition of only doing the basics unless you tell it differently, it does not follow HTTP redirects by default. Use the -L, --location option to tell it to do that. When following redirects is enabled, curl will follow up to 50 redirects by default.

CAN POST request be redirected?

You simply can't redirect a POST request via standard http methods. Are you sure that GET isn't an option here? Show activity on this post. Using a form is probably your only option as links, HTTP redirects and <meta http-equiv="refresh" > will only cause the browser to load another URL using the GET method.

Do POST requests use curl?

To POST a file with curl , simply add the @ symbol before the file location. The file can be an archive, image, document, etc.

How do I send a POST request with the curl command?

See the default CURL syntax for sending a POST request below. The -X parameter specifies the HTTP method for sending your request. In our case, we are using the POST method. To make a basic POST request with the Curl command, execute the command below on your Terminal. We can use the -d parameter to send additional data in a POST request.

What is the use of curl-X post option?

curl -X POST [options] [URL] The -X option specifies which HTTP request method will be used when communicating with the remote server. The type of the request body is indicated by its Content-Type header. Generally, a POST request is sent via an HTML form.

How do I send data from curl?

When the -F option is used, curl sends the data using the multipart/form-data Content-Type. Another way to make a POST request is to use the -d option.

Do post requests lose their data after HTTP to https redirects?

The first thing we faced with was the fact that after HTTP to HTTPS redirects – our POST requests lose their data. To reproduce it – let’s use the Python script mentioned above.


1 Answers

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
like image 83
nif Avatar answered Oct 26 '22 13:10

nif