I am building a C program that uses libcurl to send a XML file by HTTP POST. I am sending SOAP request and the response should be saved in an xml file
The program is:
#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#define URL "http://192.168.1.159:8200/ctl/ContentDir"
size_t write_data(void *ptr, size_t size, size_t nmeb, void *stream)
{
return fwrite(ptr,size,nmeb,stream);
}
size_t read_data(void *ptr, size_t size, size_t nmeb, void *stream)
{
return fread(ptr,size,nmeb,stream);
}
int main(char * Browse_request, char * response)
{
FILE * rfp = fopen("Browse_request.xml", "r");
if(!rfp) {
perror("Read File Open:");
}
FILE * wfp = fopen("response.xml", "w+"); //File pointer to write the soap response
if(!wfp) {
perror("Write File Open:");
}
struct curl_slist *header = NULL;
header = curl_slist_append (header, "Content-Type:text/xml; Charset=UTF-8");
header = curl_slist_append (header, "SOAPAction: urn:schemas-upnp-org:service:ContentDirectory:1#Browse");
header = curl_slist_append (header, "Content-Length:0");
CURL *curl;
CURLcode res;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, URL);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_data); //Reads xml file to be sent via POST operation
curl_easy_setopt(curl, CURLOPT_READDATA, rfp);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data); //Gets data to be written to file
curl_easy_setopt(curl, CURLOPT_WRITEDATA, wfp); //Writes result to file
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)-1);
curl_easy_setopt(curl, CURLOPT_VERBOSE,1L);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return 0;
}
}
It gives an error, 400 bad request.
If I do the same using command line, i am getting expected result and the result is saved in response.xml. the command that i use is:
curl -v -o response.xml -H "Content-Type: text/xml; Charset="UTF-8"" -H "SOAPAction: "urn:schemas-upnp-org:service:ContentDirectory:1#Browse"" -d @Browse_request.xml -X POST http://192.168.1.159:8200/ctl/ContentDir
my browse_request.xml which is same for command line and program is:
<?xml version = "1.0" encoding="utf-8"?>
<s:Envelope
xmlns:s = "http://schemas.xmlsoap.org/soap/envelope/"
s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:Browse xmlns:u="urn:schemas-upnp-org:Service:ContentDirectory:1">
<ObjectID>0</ObjectID>
<BrowseFlag>BrowseDirectChildren</BrowseFlag>
<Filter>*</Filter>
<StartingIndex>1</StartingIndex>
<RequestedCount>5</RequestedCount>
<SortCriteria></SortCriteria>
</u:Browse>
</s:Body>
</s:Envelope>
when i try to implement using c program, why is it giving 400 bad request
thanks in advance