gSOAP for C

Create the Stubs

Using wsdl2h to generate the header file used as an intermediate to generate the stubs. The -c option is used to to force generation of ANSI C code, and the -o option to give the generated files a more meaningful name:

wsdl2h -c -oWSDbfetchService ""

Process the generated header file (WSDbfetchService.h) with soapccp2 to generate the actual stubs:

soapcpp2 -c -pWSDbfetch -C WSDbfetchService.h

Again the -c option is used to generate ANSI C code, -p so specify more meaningful file names (e.g. WSDbfetch* rather than soap*), and -C to generate only the client stubs, by default both client stubs and server skeletons are generated.

Client Code

Now we have stubs, we need to write some code that uses them, for example (wsdbfetch_gsoap_c.c):

/* Include generated stubs */
#include "WSDbfetchH.h"
#include "WSDbfetchSoapBinding.nsmap"
  /* Arguments for query */
  char *query = "UNIPROT:WAP_RAT";
  char *format = "fasta";
  char *style = "raw";
  /* For the result */ 
  char *result = NULL;
  /* Create a service proxy */
  struct soap *soap = soap_new();
  /* Make the request, checking for failure */
  if(soap_call_ns1__fetchData(soap, NULL, NULL, query, format, style, &result) == SOAP_OK)
      /* Print the result */
      printf("%s", result);
      /* An error occurred, so print the message. */
      soap_print_fault(soap, stderr);
  /* Clean-up the proxy */

Compile the Client

Compiling the C source and linking to create an executable:

cc -o wsdbfetch_gsoap_c WSDbfetchC.c WSDbfetchClient.c wsdbfetch_gsoap_c.c -lgsoap

Complex Types

Complex types, such as the data structures used to pass the parameters and data to the runWUBlast operation of the WSWUBlast service, are mapped into structs:

  /* Create a service proxy */
  struct soap *soap = soap_new();
  /* Populate parameters */
  struct ns1__inputParams params; = "your@email";
  enum xsd__boolean asyncVal = xsd__boolean__true_;
  params.async = &asyncVal;
  params.program = "blastp";
  params.database = "swissprot";
  params.matrix = NULL;
  params.exp = NULL;
  params.echofilter = NULL;
  params.filter = NULL;
  params.numal = NULL;
  params.scores = NULL;
  params.sensitivity = NULL;
  params.sort = NULL;
  params.stats = NULL;
  params.strand = NULL;
  params.outformat = NULL;
  params.topcombon = NULL;
  params.appxml = NULL;
  /* Populate data */
  struct WSArrayofData* data = (struct WSArrayofData*)soap_malloc(soap, sizeof(struct WSArrayofData));
  data->__size = 1;
  data->__ptr = (struct ns1__data**)soap_malloc(soap, sizeof(struct ns1__data*) * data->__size);
  data->__ptr[0] = (struct ns1__data*)soap_malloc(soap, sizeof(struct ns1__data)
  data->__ptr[0]->type = "sequence";
  data->__ptr[0]->content = "uniprot:slpi_human";
  /* Submit job */
  char *jobId = NULL;
  if(soap_call_ns1__runWUBlast(soap, NULL, NULL, &params, data, &jobId) != SOAP_OK)
        /* An error occured, so print the message. */
        soap_print_fault(soap, stderr);
        exitStatus = 1;
  /* Print job identifier */
  printf("%s\n", jobId);


Complete source code for the examples are available:

tutorials/06_programming/c_cpp/soap/gsoap_c.txt · Last modified: 2011/03/29 15:32 by hpm