cURL / Mailing Lists / curl-library / Single Mail

curl-library

Building and Sending SOAP using libcurl

From: abhishek Kalapatapu <abhisrk_at_gmail.com>
Date: Sun, 11 Sep 2011 18:38:09 -0700

Here is my code for sending a SOAP using libcurl

#include <stdio.h>
#include <string.h>
#include <curl/curl.h>

/* Auxiliary function that waits on the socket. */
static int wait_on_socket(curl_socket_t sockfd, int for_recv, long
timeout_ms)
{
  struct timeval tv;
  fd_set infd, outfd, errfd;
  int res;

  tv.tv_sec = timeout_ms / 1000;
  tv.tv_usec= (timeout_ms % 1000) * 1000;

  FD_ZERO(&infd);
  FD_ZERO(&outfd);
  FD_ZERO(&errfd);

  FD_SET(sockfd, &errfd); /* always check for error */

  if(for_recv)
  {
    FD_SET(sockfd, &infd);
  }
  else
  {
    FD_SET(sockfd, &outfd);
  }

  /* select() returns the number of signalled sockets or -1 */
  res = select(sockfd + 1, &infd, &outfd, &errfd, &tv);
  return res;
}

int main(void)
{
  CURL *ch;
  CURLcode res;

  struct curl_slist *headerlist=NULL;
struct MemoryStruct *bodyStruct=NULL;
char _gatineauSoapReq[2000]="";

const char *WriteMemoryCallback;
  /* Minimalistic http request */

  curl_socket_t sockfd; /* socket */
  long sockextr;
  size_t iolen;

  ch = curl_easy_init();
  curl_easy_setopt(ch, CURLOPT_URL, "
http://thermo.sdsu.edu/servlet/ThermodynamicProperties/ThermodynamicPropertiesService
");
  curl_easy_setopt(ch, CURLOPT_POST, 1);
  curl_easy_setopt(ch, CURLOPT_HEADER, 1);
  curl_easy_setopt(ch, CURLOPT_VERBOSE, 1);
  curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
  if ((bodyStruct = (struct MemoryStruct *) malloc(sizeof(struct
MemoryStruct))) == NULL) exit(1);
  curl_easy_setopt(ch, CURLOPT_FILE, bodyStruct);

  curl_slist_free_all(headerlist);
  headerlist = curl_slist_append(headerlist, "Content-Type: text/xml");
  headerlist = curl_slist_append(headerlist, "SOAPAction: \"
http://blabla.com/blabla_services/ValidateNow\"");
  sprintf(_gatineauSoapReq, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
 <S:Body>
   <ns2:getSpeciesInformationResponse
xmlns:ns2="http://ThermodynamicProperties/">
     <return>

 {&quot;Nist&quot;:true,&quot;Abinitio&quot;:[[&quot;3-21G&quot;,&quot;MP2&quot;,&quot;&quot;]],&quot;Nasa&quot;:true,&quot;Chemkin&quot;:true,&quot;Burcat&quot;:true}
     </return>
   </ns2:getSpeciesInformationResponse>
 </S:Body>
</S:Envelope>);
  curl_easy_setopt(ch, CURLOPT_POSTFIELDS, _gatineauSoapReq);
  curl_easy_setopt(ch, CURLOPT_HTTPHEADER, headerlist);
  curl_easy_perform(ch);

    //res = curl_easy_perform(curl);

    if(CURLE_OK != res)
    {
      printf("Error: %s\n", strerror(res));
      return 1;
    }

    /* Extract the socket from the curl handle - we'll need it for waiting.
     * Note that this API takes a pointer to a 'long' while we use
     * curl_socket_t for sockets otherwise.
     */
    res = curl_easy_getinfo(ch, CURLINFO_LASTSOCKET, &sockextr);

    if(CURLE_OK != res)
    {
      printf("Error: %s\n", curl_easy_strerror(res));
      return 1;
    }

    sockfd = sockextr;

    /* wait for the socket to become ready for sending */
    if(!wait_on_socket(sockfd, 0, 60000L))
    {
      printf("Error: timeout.\n");
      return 1;
    }

    puts("Sending request.");
    /* Send the request. Real applications should check the iolen
     * to see if all the request has been sent */
    //res = curl_easy_send(curl,, strlen(request), &iolen);

    if(CURLE_OK != res)
    {
      printf("Error: %s\n", curl_easy_strerror(res));
      return 1;
    }
    puts("Reading response.");

    /* read the response */
    //for(;;)
    {
      printf("ok1 \n");
  char buf[1024];

      wait_on_socket(sockfd, 1, 60000L);
      res = curl_easy_recv(ch, buf, 1024, &iolen);
 printf("ok2 \n");
      if(CURLE_OK != res)

       //break;

   printf("Error: %s\n", strerror(res));

 //printf("ok3 \n");
      //printf("Received %u bytes.\n", iolen);
  printf("data %s \n", buf);

    }

    /* always cleanup */
    curl_easy_cleanup(ch);

  return 0;
}

The code is not compiling and throwing errors:

scurl.c:57:45: warning: incompatible implicit declaration of built-in
function âmallocâ
scurl.c:57:59: error: invalid application of âsizeofâ to incomplete type
âstruct MemoryStructâ
scurl.c:57:91: warning: incompatible implicit declaration of built-in
function âexitâ
scurl.c:63:72: warning: backslash and newline separated by space
scurl.c:64:22: error: expected â)â before âhttpâ
scurl.c:69:60: error: invalid suffix "G" on integer constant

I am new to libcurl and I am not sure whether this is the proper way of
building SOAP requests and sending it. Can anyone guide me how to proceed,
an example would be very helpful. Thanks in adavnce.

-- 
*Cheers*
*Abhishek Kalapatapu*

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2011-09-12