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>
40 static size_t cbk_curl(void *buffer, size_t size, size_t nmemb, void *userp)
45 realsize = size * nmemb;
46 mem = (struct ucontent *)userp;
48 mem->data = realloc(mem->data, mem->len + realsize + 1);
50 memcpy(&(mem->data[mem->len]), buffer, realsize);
52 mem->data[mem->len] = 0;
60 curl = curl_easy_init();
65 curl_easy_cleanup(curl);
68 char *http_get(const char *url, const char *content)
70 struct ucontent chunk;
72 chunk.data = malloc(1);
75 curl_easy_setopt(curl, CURLOPT_URL, url);
76 curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
78 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, content);
79 curl_easy_setopt(curl,
80 CURLOPT_POSTFIELDSIZE,
81 (long)strlen(content));
83 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, cbk_curl);
84 curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk);
86 if (curl_easy_perform(curl) == CURLE_OK)
90 fprintf(stderr, _("HTTP request fail url=%s"), url);
95 json_object *http_json_get(const char *url, struct json_object *j)
99 struct json_object *result;
101 in = json_object_to_json_string(j);
102 out = http_get(url, in);
105 printf("HTTP request= %s\n",
106 json_object_to_json_string(j));
109 result = json_tokener_parse(out);
111 printf("HTTP reply= %s\n", out);