[CENTOS] I tried to generate a C language program source from cURL


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);

  hnd = NULL;
  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

in conclusion

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

I tried to generate a C language program source from cURL
I tried to build Ruby 3.0.0 from source
I tried to automatically generate a class to convert from a data class to a Bundle with APT
I tried to move Hello World (+ α) written in C language from JavaScript [WebAssembly]
I tried to chew C # (indexer)
I tried to chew C # (polymorphism: polymorphism)
[Rails] I tried to raise the Rails version from 5.0 to 5.2
java I tried to break a simple block
I tried to develop a man-hour management tool
I tried to develop a DUO3.0 study website.
I tried hitting a Java method from ABCL
I tried to chew C # (basic of encapsulation)
I tried to create a LINE clone app
I tried to develop a website to record expenses.
I tried to implement a server using Netty
I tried to break a block with java (1)
I tried to make a program that searches for the target class from the process that is overloaded with Java
I tried to develop a ramen shop sharing website.
I tried to decorate the simple calendar a little
I tried to create a Clova skill in Java
I tried to make a login function in Java
What I got from continuing to develop open source
Generate models from JSON to Swift, PHP, C #, JAVA
I tried to chew C # (reading and writing files)
I want to connect to Heroku MySQL from a client
I tried to explain what you can do in a popular language for web development from a beginner's point of view.
I tried to develop a web application from a month and a half of programming learning history
Build TensorFlow 2.3.1 from source on Ubuntu20.04 LTS and create a shared library in C ++ language
I tried to make an application in 3 months from inexperienced
I tried to create a java8 development environment with Chocolatey
I tried adding a separator line to TabLayout on Android
I tried to modernize a Java EE application with OpenShift.
[JDBC] I tried to access the SQLite3 database from Java.
[Rails] I tried to create a mini app with FullCalendar
[Beginner] I made a program to sell cakes in Java
I tried to convert a string to a LocalDate type in Java
I tried to implement a buggy web application in Kotlin
I tried to make a client of RESAS-API in Java
I tried to create a padrino development environment with Docker
I tried upgrading from CentOS 6.5 to CentOS 7 with the upgrade tool
I tried source code analysis
To write a user-oriented program (1)
I tried to verify yum-cron
I tried to create an API to get data from a spreadsheet in Ruby (with service account)
I tried to create a simple map app in Android Studio
I tried to illuminate the Christmas tree in a life game
I tried to write code like a type declaration in Ruby
I tried to create a Spring MVC development environment on Mac
I am studying to change jobs from inexperienced to a web engineer.
I tried to build a simple application using Dockder + Rails Scaffold
[Java] I tried to make a maze by the digging method ♪
I tried to solve the paiza campaign problem "Challenge from Kaito 813"
I tried to build a laravel operating environment while remembering Docker
I tried to make a group function (bulletin board) with Rails
[JDBC] I tried to make SQLite3 database access from Java into a method for each SQL statement.
I tried to make a sample program using the problem of database specialist in Domain Driven Design
Ruby: I made a FizzBuzz program!
I tried a little digdag docker.run_options
A person writing C ++ tried writing Java
I tried to summarize iOS 14 support
I tried to interact with Java