Question

I'm developing an application in which I use libcurl.

I added in my c code the following curl option:

curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);

in order to follow http redirection.

when my application send a http message (first http message), it receives a http 302 redirect from the server, the libcurl detect with success the redirect information and then send another http message (second http message) to the new url but the new http message is not the same comparing to the first http message.

How I can make libcurl sending the same HTTP message?


First http message: communication between my application and the first server till the application get the http redirect message

application ----> server1

POST / HTTP/1.1
Host: 192.168.1.15
Accept: */*
User-Agent: cwmp
Content-Type: text/xml
Content-Length: 341
Expect: 100-continue

server1 ----> application

HTTP/1.1 100 Continue

application ----> server1

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <soap_env:Envelope
xmlns:soap_env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soap_enc="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <soap_env:Header>
      <cwmp:ID soap_env:mustUnderstand="1" />
    </soap_env:Header>
    <soap_env:Body>
      <any:command>any_command_value</any:command>
    </soap_env:Body>
  </soap_env:Envelope>

server1 ----> application (redirect message)

HTTP/1.1 302 Found
Date: Tue, 13 May 2014 11:00:48 GMT
Server: Apache/2.4.7 (Win32) PHP/5.5.8
X-Powered-By: PHP/5.5.8
Location: http://192.168.1.133:8080/openserv/serv
Content-Length: 0
Content-Type: text/html

Second http message: http message sent by my application to the second server after receiving the http redirect

application ---> server2

GET /openserv/serv HTTP/1.1
Host: 192.168.1.133:8080
Accept: */*
User-Agent: cwmp
Content-Type: text/xml
Was it helpful?

Solution

According to standarts it is required to send the same message on HTTP redirect. However, almost no tools implement the standart. libcurl changes request method from POST to GET on redirect, and yes, it is against standarts, but it is how it works. Possible solutions:

  1. Use CURLOPT_POSTREDIR option; it will force libcurl to at least also use POST method after redirect, I don't know what about POST request body.
  2. Recoginize 302 and 301 response codes and send request with another call to libcurl.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top