title: Sending SOAP request using libcurl
body: <p>I could understand the examples of simple sendrecv for sending REST request using libcurl. Now i want to send a SOAP request using libcurl. I have changed the REST request code to achieve the same, but the code is not working. My code is:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;curl/curl.h&gt;
  
  /* 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(&amp;infd);
    FD_ZERO(&amp;outfd);
    FD_ZERO(&amp;errfd);
  
    FD_SET(sockfd, &amp;errfd); /* always check for error */ 
  
    if(for_recv)
    {
      FD_SET(sockfd, &amp;infd);
    }
    else
    {
      FD_SET(sockfd, &amp;outfd);
    }
  
    /* select() returns the number of signalled sockets or -1 */ 
    res = select(sockfd + 1, &amp;infd, &amp;outfd, &amp;errfd, &amp;tv);
    return res;
  }
  
  int main(void)
  {
    CURL *curl;
    CURLcode res;
    /* Minimalistic http request */ 
    const char *request = "&lt;?xml version="1.0" encoding="utf-8"?&gt;
                          &lt;S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"
                          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                          xmlns:tns="http://ThermodynamicProperties/"&gt;
                          &lt;S:Body&gt;
                          &lt;tns:getSpeciesInformation&gt;
                          &lt;speciesSymbol&gt;CO2&lt;/speciesSymbol&gt;
                          &lt;phase&gt;GAS&lt;/phase&gt;
                          &lt;/tns:getSpeciesInformation&gt;
                          &lt;/S:Body&gt;
                          &lt;/S:Envelope&gt;";
    curl_socket_t sockfd; /* socket */ 
    long sockextr;
    size_t iolen;
  
    curl = curl_easy_init();
    if(curl) {
      curl_easy_setopt(curl, CURLOPT_URL, "http://thermo.sdsu.edu/servlet/ThermodynamicProperties/ThermodynamicPropertiesService");
      curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
      curl_easy_setopt(curl, CURLOPT_POST, 1);
      curl_easy_setopt(curl, CURLOPT_POSTFIELDS, request);
      /* Do not do the transfer - only connect to host */ 
      curl_easy_setopt(curl, CURLOPT_CONNECT_ONLY, 1L);
  
  
      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(curl, CURLINFO_LASTSOCKET, &amp;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,request, strlen(request), &amp;iolen);
  
      if(CURLE_OK != res)
      {
        printf("Error: %s\n", curl_easy_strerror(res));
        return 1;
      }
  </code></pre>
  
  <p>I feel that I am not doing correct in building the SOAP request. Any help would be great as this is a completely new field for me.</p>