Help us understand the problem. What is going on with this article?

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

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[] = "http://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"
  }, 
  "json": null, 
  "origin": "153.143.162.1, 153.143.162.1", 
  "url": "https://httpbin.org/post"
}

*** 終了 ***

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

$ clang++ --version
clang version 8.0.0 (tags/RELEASE_800/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした