diff options
author | Iru Cai <mytbk920423@gmail.com> | 2018-06-27 22:56:25 +0800 |
---|---|---|
committer | Iru Cai <mytbk920423@gmail.com> | 2018-06-27 22:56:25 +0800 |
commit | e6d9edc048efb5b19d79bbe7871b2ff779d72311 (patch) | |
tree | 8d9eedd599f09d7aa9b64ab6251b9b9a092fce91 /util.c | |
download | matrix-curl-e6d9edc048efb5b19d79bbe7871b2ff779d72311.tar.xz |
initial code
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); +} |