0

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

sri
  • 43
  • 9
  • Try stepping through your program in a debugger, adding debug prints or even just indenting the code properly. You may find that your main function does not do anything. – Jussi Kukkonen Dec 08 '16 at 13:24
  • 1
    The code that you have provided does not do anything but define a nested function `sendMessage()` in `main()`. [C does not supported nested functions](http://stackoverflow.com/questions/2608158/nested-function-in-c) (though they can be a compiler-dependent extension), but ignoring this, `sendMesage()` is never called, and nothing else, not even `return`ing 0, is done in `main()`. – ad absurdum Dec 08 '16 at 14:29
  • thanks david bowling. it works but now it gives an error, 400 bad request. – sri Dec 09 '16 at 09:16
  • @sri now your question is different than it was before, please ask a new question instead of changing this one... but before you do that do compare the messages you send (from command line and from your app), you can do that with e.g. wireshark. Content-Length as an example is not going to be the same. – Jussi Kukkonen Dec 09 '16 at 09:25

0 Answers0