I wanted to make a program that issues Web service requests with a C program, but it was troublesome to make it from scratch, so after investigating various things, it seems that the C source can be generated with the cURL function, so I tried it I did.
① Source generation (Issue a request with curl. Generate the requested content executed by adding the -libcurl option as a C source)
curl -H "Content-Type: text/xml; charset=utf-8" http://ipaddr:port_number/soap/base64t -X POST -d @base64_soap.txt --libcurl post_base64.c
② Check the generated source (it looks like this)
/********* Sample code generated by the curl command line tool **********
* All curl_easy_setopt() options are documented at:
* http://curl.haxx.se/libcurl/c/curl_easy_setopt.html
************************************************************************/
#include <curl/curl.h>
int main(int argc, char *argv[])
{
CURLcode ret;
CURL *hnd;
struct curl_slist *slist1;
slist1 = NULL;
slist1 = curl_slist_append(slist1, "Content-Type: text/xml; charset=utf-8");
hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_URL, "http://ipaddr:port_number/soap/base64t");
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" ><SOAP-ENV:Body><BASE64TOperation xmlns=\"http://www.BASE64T.BASE64T.Request.com\"><comm_args><debug_mode></debug_mode></comm_args><comm_data><strelem_0001>YWJjZGVmZw=B</strelem_0001></comm_data></BASE64TOperation></SOAP-ENV:Body></SOAP-ENV:Envelope>");
curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)326);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.29.0");
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_SSH_KNOWNHOSTS, "/root/.ssh/known_hosts");
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_TCP_KEEPALIVE, 1L);
/* Here is a list of options the curl code used that cannot get generated
as source easily. You may select to either not use them or implement
them yourself.
CURLOPT_WRITEDATA set to a objectpointer
CURLOPT_WRITEFUNCTION set to a functionpointer
CURLOPT_READDATA set to a objectpointer
CURLOPT_READFUNCTION set to a functionpointer
CURLOPT_SEEKDATA set to a objectpointer
CURLOPT_SEEKFUNCTION set to a functionpointer
CURLOPT_ERRORBUFFER set to a objectpointer
CURLOPT_STDERR set to a objectpointer
CURLOPT_HEADERFUNCTION set to a functionpointer
CURLOPT_HEADERDATA set to a objectpointer
*/
ret = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);
hnd = NULL;
curl_slist_free_all(slist1);
slist1 = NULL;
return (int)ret;
}
/**** End of sample code ****/
(3) After that, if you compile this source by linking the curl library, you can execute the contents executed by curl from the C program.
# gcc post_base64.c -o post_base64 -lcurl
# ls | grep post
post_base64
post_base64.c
This time, I tried it on CentOS. Also, when issuing a request with cURL, a separate SOAP file (@ base64_soap.txt) is prepared. If you want to generate C source with JSON request, change the URL of the service to be called and the header part to -H "Content-Type: application / json; charset = UTF-8" and prepare JSON format data in a separate file. You should be able to do it.
Recommended Posts