Created
June 4, 2017 20:06
-
-
Save BonfaceKilz/d3a3cc34ad4f4c70e9cdd247f8cb675c to your computer and use it in GitHub Desktop.
A program that uses libcurl to fetch data in JSON format. Later parsed with json-c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A demo of how to get data from a website in JSON format | |
* I'll get data from this site: | |
* http://jsonplaceholder.typicode.com/posts | |
* | |
* Requirements: | |
* | |
* json-c - https://github.com/json-c/json-c | |
* libcurl - http://curl.haxx.se/libcurl/c | |
* | |
* Build: | |
* | |
* gcc curltest.c -lcurl -ljson-c -o curltest | |
* | |
* Run: | |
* | |
* ./curltest | |
* | |
*/ | |
/* standard includes */ | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
/* json-c (https://github.com/json-c/json-c) */ | |
#include <json-c/json.h> | |
/* libcurl (http://curl.haxx.se/libcurl/c) */ | |
#include <curl/curl.h> | |
struct MemoryStruct { | |
char *memory; | |
size_t size; | |
}; | |
static size_t WriteMemoryCallback(void *contents, size_t size, size_t nmemb, void *userp) | |
{ | |
size_t realsize = size * nmemb; | |
struct MemoryStruct *mem = (struct MemoryStruct *)userp; | |
mem->memory = realloc(mem->memory, mem->size + realsize + 1); | |
if(mem->memory == NULL) { | |
/* out of memory! */ | |
printf("not enough memory (realloc returned NULL)\n"); | |
return 0; | |
} | |
memcpy(&(mem->memory[mem->size]), contents, realsize); | |
mem->size += realsize; | |
mem->memory[mem->size] = 0; | |
return realsize; | |
} | |
int main() | |
{ | |
/* url to test site */ | |
char *url = "http://jsonplaceholder.typicode.com/posts/"; | |
CURL *curl_handle; | |
CURLcode res; | |
struct MemoryStruct chunk; | |
chunk.memory = malloc(1); | |
chunk.size = 0; | |
curl_global_init(CURL_GLOBAL_ALL); | |
curl_handle = curl_easy_init(); | |
curl_easy_setopt(curl_handle, CURLOPT_URL, url); | |
curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, WriteMemoryCallback); | |
curl_easy_setopt(curl_handle, CURLOPT_WRITEDATA, (void *)&chunk); | |
curl_easy_setopt(curl_handle, CURLOPT_USERAGENT, "libcurl-agent/1.0"); | |
/* get it! */ | |
res = curl_easy_perform(curl_handle); | |
//printf("%s", chunk.memory); | |
/* check for errors */ | |
if(res != CURLE_OK) { | |
fprintf(stderr, "curl_easy_perform() failed: %s\n", | |
curl_easy_strerror(res)); | |
} | |
else { | |
printf("%lu bytes retrieved\n", (long)chunk.size); | |
char *mystring = chunk.memory; | |
// printf("%s", mystring); | |
json_object *jobj = json_tokener_parse(mystring); | |
printf("jobj from str:\n---\n%s\n---\n", json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_SPACED | JSON_C_TO_STRING_PRETTY)); | |
} | |
/* cleanup curl stuff */ | |
curl_easy_cleanup(curl_handle); | |
free(chunk.memory); | |
/* we're done with libcurl, so clean it up */ | |
curl_global_cleanup(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment