Reputation: 2084
I am trying to POST xml data from a c program using libcurl to a website. When I use the command line program in linux, curl like this it works fine:
curl -X POST -H 'Content-type: text/xml' -d 'my xml data' http://test.com/test.php
(I changed the actual data for security)
But as soon as I try to write c code using libcurl, it fails almost every time but succeeds every once in a while. Here is my c code:
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl)
{
curl_easy_init(curl, CURLOPT_URL, "http://test.com/test.php");
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, xmlString.c_str());
curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
I have this code in a loop that runs about every 10 seconds and it will only succeed about every 4 or 5 calls. I get back errors from the server that say "XML Head not found".
I tried specifying the HTTP header with:
struct curl_slist *chunk = NULL
chunk = curl_slist_append(chunk, "Content-type: text/xml");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk);
But I didn't have any luck. Any ideas?
Upvotes: 3
Views: 4274
Reputation: 596948
Try this:
CURL *curl = curl_easy_init();
if(curl)
{
curl_easy_setopt(curl, CURLOPT_URL, "http://test.com/test.php");
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, xmlString.c_str());
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, xmlString.length());
struct curl_slist *slist = curl_slist_append(NULL, "Content-Type: text/xml; charset=utf-8"); // or whatever charset your XML is really using...
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, slist);
curl_easy_perform(curl);
curl_slist_free_all(slist);
curl_easy_cleanup(curl);
}
Upvotes: 8