2 * Copyright (C) 2010-2013 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #define _(str) gettext(str)
27 #include <curl/curl.h>
28 #include <json/json.h>
39 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
44 realsize = size * nmemb;
45 mem = (struct ucontent *)userp;
47 mem->data = realloc(mem->data, mem->len + realsize + 1);
49 memcpy(&(mem->data[mem->len]), buffer, realsize);
51 mem->data[mem->len] = 0;
58 curl = curl_easy_init();
63 curl_easy_cleanup(curl);
66 json_object *get_json_object(const char *url)
68 struct ucontent chunk;
76 chunk.data = malloc(1);
79 curl_easy_setopt(curl, CURLOPT_URL, url);
80 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
81 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
82 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
84 if (curl_easy_perform(curl) == CURLE_OK)
85 obj = json_tokener_parse(chunk.data);
87 fprintf(stderr, _("Fail to connect to: %s"), url);
94 static json_object *post(const char *url, const char *body)
96 struct ucontent chunk;
101 chunk.data = malloc(1);
104 curl_easy_setopt(curl, CURLOPT_URL, url);
105 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
106 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
107 curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(body));
108 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
109 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
111 if (curl_easy_perform(curl) == CURLE_OK)
112 obj = json_tokener_parse(chunk.data);
114 fprintf(stderr, _("Fail to connect to: %s"), url);
120 json_object *post_json_object(const char *url, struct json_object *j)
122 return post(url, json_object_to_json_string(j));