LoginSignup
7
9

More than 1 year has passed since last update.

C++ の http クライアントの使い方 (Post)

Last updated at Posted at 2018-05-26
http_post.cpp
// --------------------------------------------------------------------
/*
	http_post.cpp

					May/27/2018

*/
// --------------------------------------------------------------------
#include	<string>
#include	<iostream>
#include	<cstring>

#include	<curl/curl.h>

using namespace std;

// --------------------------------------------------------------------
size_t callBackFunk(char* ptr, size_t size, size_t nmemb, string* stream)
{
	int realsize = size * nmemb;
	stream->append(ptr, realsize);
	return realsize;
}

// --------------------------------------------------------------------
string url_post_proc (const char url[],const char post_data[])
{
	CURL *curl;
	CURLcode res;
	curl = curl_easy_init();
	string chunk;

	if (curl)
		{
		curl_easy_setopt(curl, CURLOPT_URL, url);
		curl_easy_setopt(curl, CURLOPT_POST, 1);
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);
		curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(post_data));
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callBackFunk);
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (string*)&chunk);
		curl_easy_setopt(curl, CURLOPT_PROXY, "");
		res = curl_easy_perform(curl);
		curl_easy_cleanup(curl);
		}
	if (res != CURLE_OK) {
		cout << "curl error" << endl;
		exit (1);
	}

	return chunk;
}

// --------------------------------------------------------------------
int main (int argc,char *argv[])
{
	cerr << "*** 開始 ***\n";

	char url_target[] = "https://httpbin.org/post";
	char post_data[] = "user=jiro&password=123456";

	string str_out = url_post_proc (url_target,post_data);

	cout << str_out << "\n";

	cerr << "*** 終了 ***\n";

	return 0;
}

// --------------------------------------------------------------------
Makefile
http_post.exe: http_post.cpp
	clang++ -o http_post http_post.cpp -lcurl
clean:
	rm -f http_post

実行結果

$ ./http_post 
*** 開始 ***
{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "password": "123456", 
    "user": "jiro"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "25", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org", 
    "X-Amzn-Trace-Id": "Root=1-5f25462e-4f05abfe4fb47190dc0ef00c"
  }, 
  "json": null, 
  "origin": "219.126.139.62", 
  "url": "https://httpbin.org/post"
}

*** 終了 ***

次のバージョンで確認しました。

$ clang++ --version
Ubuntu clang version 15.0.2-1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
7
9
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
7
9