views:

468

answers:

1

Hi

I'm facing a problem with curl as I am unable to issue a PUT request with inline XML data, I'm not sure how its done but I hade a couple of goes on it with different techniques. First I tried using the CURLOPT_UPLOAD as its the default CURL option for PUT and tried to append the xml data manually:

    typedef map<string, string> headers_t;
    std::string strCommand = <XMLCOMMAND>

PUTRequest(param1, param2, ...)
    {
...
    headers_t headers;
    int nLen = strCommand.length();
    stringstream issLen;
        issLen << nLen;
        issLen >> strln;
    curl_easy_setopt(curl, CURLOPT_UPLOAD, true); // HTTP PUT
    headers.append("Content-Length: "+ strln); //
    headers.append(strCommand);
...
    }

Then I tried the same method but using the CURLOPT_POSTFIELDS and CURLOPT_POSTFIELDSIZE instead of manually appending the command to the HTTP headers.... did not work.

Then I tried customizing the PUT request using the CURLOPT_CUSTOMREQUESToption and setting the parameter to PUT and also manually appending the command and using the POSTFIELDS method.

Sadly none worked and now I'm clueless as of what to try next.

+1  A: 

When using CURLOPT_UPLOAD, you are appending the XML to the headers of the request rather then to the body where it belongs. You need to use CURLOPT_READDATA (with CURLOPT_READFUNCTION if your XML is not in a file) to provide the XML data when curl asks for it, and also use CURLOPT_INFILESIZE/CURLOPT_INFILESIZE_LARGE so curl can generate a proper 'Content-Length' header (don't append that header manually).

If you use CURLOPT_POST, then use CURLOPT_POSTFIELDS and CURL_POSTFIELDSIZE/CURLOPT_POSTFIELDSIZE_LARGE to provide the actual XML data, but then you also have to use CURLOPT_HTTPHEADER to override the default 'Content-Type' header so you can change it from the default value of 'application/x-www-form-urlencoded' to 'text/xml' instead.

Remy Lebeau - TeamB