http client - libcurl
참고:
- libcurl - https://curl.haxx.se/libcurl/
- libcurl get - https://curl.haxx.se/libcurl/c/simple.html
- libcurl post - https://curl.haxx.se/libcurl/c/post-callback.html
- HTTP test server - https://httpbin.org/
Http get sample
- http get
- 응답 데이터를 .out 파일로 저장
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
static size_t cb_write(void * contents, size_t size, size_t nmemb, void * userp)
{
FILE * fp = (FILE*)userp;
fwrite(contents, size, nmemb, fp);
return size * nmemb;
}
static void dumpfile(const char * path);
int main(int argc, char *argv[])
{
FILE * out;
CURLcode res;
CURL * curl;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (!curl) {
printf("curl_easy_init() failed\n");
exit(1);
}
out = fopen(".out", "wb");
if (!out) {
printf("fopen() failed\n");
exit(1);
}
curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/get");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb_write);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)out);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
fclose(out);
printf("curl_easy_perform() failed\n");
exit(1);
}
fclose(out);
dumpfile(".out");
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}
static void dumpfile(const char * path) {
int len;
char buf[1024] = {0,};
FILE * fp = fopen(path, "rb");
if (!fp) {
perror("fopen() failed");
exit(1);
}
while ((len = fread(buf, sizeof(char), sizeof(buf), fp)) > 0) {
printf("%.*s", len, buf);
}
fclose(fp);
}
빌드
$ gcc -o httpget main.c `pkg-config --cflags --libs libcurl`
실행
$ ./httpget
{
"args": {},
"headers": {
"Accept": "*/*",
"Connection": "close",
"Host": "httpbin.org"
},
"origin": "xx.xx.xx.xx",
"url": "http://httpbin.org/get"
}
Http post sample
- http post
- Content-Type: text/plain
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
static size_t cb_write(void * contents, size_t size, size_t nmemb, void * userp)
{
FILE * fp = (FILE*)userp;
fwrite(contents, size, nmemb, fp);
return size * nmemb;
}
static void dumpfile(const char * path);
int main(int argc, char *argv[])
{
struct curl_slist * headers = NULL;
static const char * msg = "hello world";
FILE * out;
CURLcode res;
CURL * curl;
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if (!curl) {
printf("curl_easy_init() failed\n");
exit(1);
}
out = fopen(".out", "wb");
if (!out) {
printf("fopen() failed\n");
exit(1);
}
headers = curl_slist_append(headers, "Content-Type: text/plain");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/post");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, msg);
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, strlen(msg));
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb_write);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)out);
res = curl_easy_perform(curl);
curl_slist_free_all(headers);
if (res != CURLE_OK) {
fclose(out);
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
exit(1);
}
fclose(out);
dumpfile(".out");
curl_easy_cleanup(curl);
curl_global_cleanup();
return 0;
}
static void dumpfile(const char * path) {
int len;
char buf[1024] = {0,};
FILE * fp = fopen(path, "rb");
if (!fp) {
perror("fopen() failed");
exit(1);
}
while ((len = fread(buf, sizeof(char), sizeof(buf), fp)) > 0) {
printf("%.*s", len, buf);
}
fclose(fp);
}
빌드
$ gcc -o httppost main.c `pkg-config --cflags --libs libcurl`
실행
$ ./httppost
{
"args": {},
"data": "hello world",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Connection": "close",
"Content-Length": "11",
"Content-Type": "text/plain",
"Host": "httpbin.org"
},
"json": null,
"origin": "xx.xx.xx.xx",
"url": "http://httpbin.org/post"
}
댓글 없음:
댓글 쓰기