3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "convert_json.h"
29 #include "httpfetch.h"
32 Json::Value fetchJsonValue(const std::string &url,
33 std::vector<std::string> *extra_headers)
35 HTTPFetchRequest fetch_request;
36 HTTPFetchResult fetch_result;
37 fetch_request.url = url;
38 fetch_request.caller = HTTPFETCH_SYNC;
40 if (extra_headers != NULL)
41 fetch_request.extra_headers = *extra_headers;
43 httpfetch_sync(fetch_request, fetch_result);
45 if (!fetch_result.succeeded) {
50 std::istringstream stream(fetch_result.data);
52 if (!reader.parse(stream, root)) {
53 errorstream << "URL: " << url << std::endl;
54 errorstream << "Failed to parse json data " << reader.getFormattedErrorMessages();
55 if (fetch_result.data.size() > 100) {
56 errorstream << "Data (" << fetch_result.data.size()
57 << " bytes) printed to warningstream." << std::endl;
58 warningstream << "data: \"" << fetch_result.data << "\"" << std::endl;
60 errorstream << "data: \"" << fetch_result.data << "\"" << std::endl;