Dijkstra Dijkstra - 2 months ago 11
C++ Question

Using libCurl to POST adds unknown header and footer

I'm using libCurl in c++ to make a POST request of an xml file to my server. The post works, I receive the xml on my server. However the xml also has a strange header & footer around the xml:

------------------------------b6966127f870Content-Disposition: form-data; name="myName"; filename="myFile.xml"Content-Type: application/xml<CORRECT XML FILE HERE>------------------------------b6966127f870--


What is this header/footer?

Can I get rid of it? Or should I just parse around it?

Is curl adding these?

Here are the relevant curl calls I make to post the xml file.

void CurlUtils::postFileToURL(const char* const inFile,
const char* const urlString)
{
// Setup
CURL* const curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);

// Post
struct curl_httppost* post = NULL;
struct curl_httppost* last = NULL;
curl_formadd(&post, &last,
CURLFORM_COPYNAME, "myName",
CURLFORM_FILE, inFile,
CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_URL, urlString);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, post);
curl_easy_perform(curl);

// Cleanup ...
}

Answer

You're using CURLOPT_HTTPPOST which makes a multipart formpost to the server. A multipart formpost is a series of parts with MIME style separators and a set of headers for each part, exactly what you see there.

If you want a plain POST without the multipart stuff, use CURLOPT_POSTFIELDS or set a read callback and use CURLOPT_POST.