diff options
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 36 |
1 files changed, 36 insertions, 0 deletions
@@ -0,0 +1,36 @@ +#include "util.h" + +static size_t get_json_callback(void *contents, size_t size, size_t nmemb, void *userp) +{ + size_t realsize = size * nmemb; + + json_tokener *tok = json_tokener_new(); + if (userp != NULL) + *((json_object **)userp) = json_tokener_parse_ex(tok, contents, realsize); + + json_tokener_free(tok); + + return realsize; +} + +CURLcode +_curl_get(CURL *curl, const char *url, json_object **resp) +{ + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_json_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)resp); + + return curl_easy_perform(curl); +} + +CURLcode +_curl_post(CURL *curl, const char *url, const char *postdata, json_object **resp) +{ + curl_easy_setopt(curl, CURLOPT_URL, url); + curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postdata); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, get_json_callback); + curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)resp); + + return curl_easy_perform(curl); +} |