libcurl call https post

C++ 2018. 11. 9. 22:42


Makefile

CXX = g++

CXXFLAGS = -g -O0 -W -Wall -std=c++11 -pthread 

#CXXFLAGS = -O2 -DNDEBUG -std=c++11 -pthread -I -Wno-unused

TARGET = out

SRCS = $(wildcard *.cpp) $(wildcard *.c)

OBJS = $(SRCS:.cpp=.o)

INC = -I/home/ky/tmp/curl/curl-7.62.0/include

LIB_DIRS = -L/usr/local/lib

LIBS = -lcurl 



all: $(TARGET)

$(CXX) -o $(TARGET) $(OBJS) $(INC) $(LIB_DIRS) $(LIBS)


$(TARGET):

$(CXX) -c $(SRCS) $(CXXFLAGS) $(INC) $(LIB_DIRS) $(LIBS)



clean: 

@rm -rf $(TARGET)

@find  -name '*.o' -exec rm {} \;


test:

@echo ${JAVA_HOME}



run.sh

#!/bin/bash


clear && make clean && make && ./out




main.cpp


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <string>

#include <curl/curl.h>


class MemoryStruct {

public:

char* memory;

size_t size;


public:

MemoryStruct() {

memory = (char*)malloc(1);

size = 0;

}

};


static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) {

char* str = static_cast<char*>(contents);

size_t realsize = size * nmemb;


std::string& res = *(static_cast<std::string*>(userp));

res.append(str, realsize);


return realsize;

}


static std::string get_client_token(const std::string& json) {

const char* find_str = "client_token\":\"";


char* str = const_cast<char*>(json.c_str());


char* spos = strstr(str, find_str);

if (spos == nullptr) return "";

spos += strlen(find_str);


char* epos = strchr(spos, '"');

if (epos == nullptr) return "";


int size = static_cast<int>(epos - spos);

return std::string(spos, size);

}


int main() {

printf("run\n");


std::string test = "{\"request_id\":\"first\",\"lease_id\":\"\",\"renewable\":false,\"lease_duration\":0,\"data\":{},\"wrap_info\":null,\"warnings\":[\"custom warning\"],\"auth\":{\"client_token\":\"tokennnnnn\",\"accessor\":\"aaaaaaa\",\"policies\":[\"default\",\"t\"],\"token_policies\":[\"default\",\"aa\"],\"metadata\":{\"username\":\"user1\"},\"lease_duration\":600,\"renewable\":true,\"entity_id\":\"asdf13f\"}}";

std::string client_token = get_client_token(test);


printf("%s\n", client_token.c_str());

return 0;



CURL* curl = nullptr;


curl_global_init(CURL_GLOBAL_DEFAULT);


curl = curl_easy_init();

if (!curl) {

printf("curl is null\n");

return -1;

}


// agent

curl_easy_setopt(curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");


// header

struct curl_slist* header = nullptr;

header = curl_slist_append(header, "Content-Type: application/json");

curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header);


// url

const char* url = "https://url:port/login/user1";

curl_easy_setopt(curl, CURLOPT_URL, url);


// post data

const char* post_data = "{\"password\": \"암호\"}";

curl_easy_setopt(curl, CURLOPT_POST, 1L);

curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data);


// insecure

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);


// get data

std::string data;

curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);

curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data);


CURLcode res = curl_easy_perform(curl);

if (res != CURLE_OK) {

printf("fail res %s", curl_easy_strerror(res));

curl_easy_cleanup(curl);

return -1;

}


printf("%lu\n%s", data.size(), data.c_str());





curl_easy_cleanup(curl);

curl_slist_free_all(header);


curl_global_cleanup();




printf("done....\n");

return 0;

}