- Get 방식  - 요청/응답 ( "http://127.0.0.1:8080/helloworld")

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <curl/curl.h>
 
void request_get_helloworld(void) {
    CURL *curl;
    CURLcode res;
 
    struct memory data;
    char url[100={0};
 
    memset(&data, 0sizeof(data));
 
    curl = curl_easy_init();
    if(curl) {
        sprintf(url, "http://127.0.0.1:8080/helloworld");
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data);
 
        res = curl_easy_perform(curl);
        if(CURLE_OK == res) {
            long status_code = 0;
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status_code);
 
            printf("[status line] Status Code %ld \n", status_code);
            printf("[body] %s \n", data.response);            
        }else {
            printf("response is not OK : %d \n", res);
        }
    }
 
    /* always cleanup */
    curl_easy_cleanup(curl);
}
cs

- POST 방식 - 요청/응답 ( "http://127.0.0.1:8080/helloworld")

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <curl/curl.h>
 
void request_post_helloworld(void) {
 
   CURL *curl;
   CURLcode res;
 
    struct memory data;
    char url[100={0};
    char post[100={0};
 
    memset(&data, 0sizeof(data));
 
    curl = curl_easy_init();
    if(curl) {
        sprintf(url, "http://127.0.0.1:8080/helloworld");
        sprintf(post, "Hello World");
 
        curl_easy_setopt(curl, CURLOPT_URL, url);
        curl_easy_setopt(curl, CURLOPT_POST, 1L); //POST request
        curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post); //POST request payload
        curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long) strlen(post)); //POST request payload size
 
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cb);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&data);
 
        res = curl_easy_perform(curl);
        if(CURLE_OK == res) {
            long status_code = 0;
            curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status_code);
 
            printf("[status line] Status Code %ld \n", status_code);
            printf("[body] %s \n", data.response);            
        }else {
            printf("response is not OK : %d \n", res);
        }
    }
 
    /* always cleanup */
    curl_easy_cleanup(curl);
 
}
cs

'프로그래밍 > C언어' 카테고리의 다른 글

코드에서 외부프로그램 실행  (0) 2022.07.11
Microhttpd 를 이용한 Post 구현  (0) 2022.07.11
Json C로 Json <-> string 변환  (0) 2022.07.11
연결리스트로 스택 만들기  (0) 2022.07.10
연결 리스트로 큐 생성  (0) 2022.07.10

+ Recent posts