C 에서 json-c와 curl 사용해서 REST API 사용하기
by
C에서 json을 사용하려면?
json-c라는 라이브러리가 있습니다. json 객체를 만들어서 request를 보낼 수도 있고, json 형식으로 response를 파싱할 수도 있습니다.
json-c 라이브러리를 사용해서 gcc로 컴파일할 때에는 -ljson-c 옵션을 붙여줍니다.
https://m.blog.naver.com/PostView.nhn?blogId=yababies&logNo=220029067066&proxyReferer=https:%2F%2Fwww.google.com%2F
C에서 libcurl 사용해서 HTTP request 보내기
cURL(client URL)은 터미널에서 서버와 통신할 수 있게 해주는 툴인데 HTTP를 포함해서 여러 프로토콜을 지원합니다. URL로 할 수 있는 건 모두 할 수 있다고 합니다.
libcurl 라이브러리를 사용해서 gcc로 컴파일할 때에는 -lcurl 옵션을 붙여줍니다.
command line에서 curl 사용 시, --libc code.c
를 붙여주면 해당 curl request를 C 코드로 만들어줍니다.
예시: google vision api 를 사용하려는 예시입니다.
curl -X POST \
-H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
-H "Content-Type: application/json; charset=utf-8" \
https://vision.googleapis.com/v1/images:annotate -d @request.json --libc code.c
커맨드라인에서 curl로 이런 걸 했을 때, code.c가 아래와 같이 생성됩니다. 저 curl_easy_setopt를 통해서 설정하는 옵션이 다 필요한 건지는 모르겠는데(몇 개 없어도 돌아갑니다), 자동으로 만들어주니까 편합니다.
/********* Sample code generated by the curl command line tool **********
* All curl_easy_setopt() options are documented at:
* https://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, "Authorization: Bearer ");
slist1 = curl_slist_append(slist1, "Content-Type: application/json; charset=utf-8");
hnd = curl_easy_init();
curl_easy_setopt(hnd, CURLOPT_BUFFERSIZE, 102400L);
curl_easy_setopt(hnd, CURLOPT_URL, "https://vision.googleapis.com/v1/images:annotate");
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 1L);
curl_easy_setopt(hnd, CURLOPT_POSTFIELDS, "[사진 이미지를 인코딩한 문자열 너무 길어서 생략]");
curl_easy_setopt(hnd, CURLOPT_POSTFIELDSIZE_LARGE, (curl_off_t)105928);
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, slist1);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.68.0");
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50L);
curl_easy_setopt(hnd, CURLOPT_HTTP_VERSION, (long)CURL_HTTP_VERSION_2TLS);
curl_easy_setopt(hnd, CURLOPT_SSH_KNOWNHOSTS, "/home/silverysky/.ssh/known_hosts");
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, "POST");
curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 1L);
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_INTERLEAVEDATA 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 ****/
json-c와 curl 사용
json_object
들을 만들어서 잘 조합시키고 그걸 string으로 바꿔서 request 보낼 수 있습니다.
Subscribe via RSS