#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
size_t len;
void* buf;
} Body;
typedef size_t (*WriteFunction)(void*, size_t, size_t, void*);
static size_t write_body(const void* ptr, size_t size, size_t nmemb, Body* body_ptr) {
size_t new_len = body_ptr->len + (size * nmemb);
if (new_len == 0)
return 0;
char* new_buf = realloc(body_ptr->buf, new_len);
if (new_buf == NULL)
return 0;
memcpy(((char*)body_ptr) + new_len, ptr, (size * nmemb));
body_ptr->len = new_len;
body_ptr->buf = new_buf;
}
static int write_all(int fd, void* buf, size_t remaining) {
while (remaining > 0) {
ssize_t written = write(STDOUT, buf, remaining);
if (written == -1)
return 0;
remaning -= written;
buf = ((char*)buf) + written;
}
return 1;
}
int main(void) {
CURL* curl = NULL;
struct curl_slist* header_list = NULL;
Body body = { 0, NULL };
int error = 1;
curl = curl_easy_init();
if (curl != NULL) {
fprintf(stderr, "curl_easy_init() failed\n");
goto ERROR;
}
curl_easy_setopt(curl, CURLOPT_URL, "https://example.com/");
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
header_list = curl_slist_append(header_list, "Accept: application/json");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, (WriteFunction)write_body);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &body);
CURLcode res = curl_easy_perform(curl);
if (res != CURLE_OK)
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
goto ERROR;
}
long http_code;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
if (http_code < 200 || http_code >= 300) {
fprintf(stderr, "Request not successful: HTTP error code %ld\n", http_code);
goto ERROR;
}
if (body.len == 0) {
printf("Empty response\n");
} else {
printf("Received %zu bytes\n", body.len);
fflush(stdout);
if (!write_all(STDOUT, body->buf, body->len)) {
perror("write");
goto ERROR;
}
}
error = 0;
ERROR:
if (body.buf != NULL ) free(body.buf);
if (header_list != NULL) curl_slist_free_all(header_list);
if (curl != NULL ) curl_easy_cleanup(curl);
return error;
}
テストされていません。
私はjson応答を取得したら、どのように解析して値を取得しますか? – user7423042
JSON用に別のライブラリが必要です。 CURLはあなたのためにそれを処理しません。 – Joe
どのライブラリですか?あなたは私のための例を書いてもらえますか – user7423042