LoginSignup
34
37

More than 5 years have passed since last update.

cURL からC言語のプログラム・ソースを生成してみた

Last updated at Posted at 2016-10-31

背景

CのプログラムでWebサービスのリクエストを発行するプログラムを作りたかったのですが、一から作るのが面倒だったので、いろいろと調べてたらcURLの機能でCのソースを生成できるらしく、やってみました。

手順

①ソースの生成
(curlでリクエストを発行。-libcurlオプションを付加することで実行したリクエスト内容をCのソースとして生成)

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

②生成したソースを確認 (こんな感じになります)

/********* 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 ****/

③あとはこのソースをcurlのライブラリーをリンクさせてコンパイルすれば、curlで実行した内容がCのプログラムから実行できます。
※gccつかいました。

# gcc post_base64.c -o post_base64 -lcurl
# ls | grep post
post_base64
post_base64.c

おわりに

今回、CentOS上でためしてみました。
また、cURLでリクエストを発行する際、SOAPメッセージを別ファイル(@base64_soap.txt)を用意しています。
JSONリクエストとかでCソースを生成したい場合、呼び出すサービスのURLと、ヘッダー部分を-H "Content-Type: application/json; charset=UTF-8"に変えて、JSON形式のデータを別ファイルで用意してあげればできるはずです。

34
37
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
37