Json: parse error - unexpected end of input

Created on 18 Apr 2017  路  4Comments  路  Source: nlohmann/json

#include <cpprest/http_client.h>
#include <nlohmann/json.hpp>
#include <iostream>
int main() {
     web::uri_builder builder("http://xkcd.com/614/info.0.json");   // first builder
     // web::uri_builder builder("http://i.jandan.net/?oxwlxojflwblxbsapi=jandan.get_duan_comments?page=25");   // second builder
        web::http::client::http_client client(builder.to_uri());
        nlohmann::json json = nlohmann::json::parse(client.request(web::http::methods::GET)
                              .get().extract_string().get());
        std::cout << json << std::endl;
}

Second builder can run.
But when run first builder, i will get:
terminate called after throwing an instance of 'std::invalid_argument'
what(): parse error - unexpected end of input
[1] 2056 abort (core dumped) ./bin/xkcd_bot

Please help me! Thanks very much!

question proposed fix

Most helpful comment

Can you make sure that the HTTP client is following redirections? I get the following output with httpie;

$ http http://xkcd.com/614/info.0.json
HTTP/1.1 301 Moved Permanently
Accept-Ranges: bytes
Connection: close
Content-Length: 0
Date: Wed, 19 Apr 2017 17:02:46 GMT
Location: https://xkcd.com/614/info.0.json
Retry-After: 0
Server: Varnish
Via: 1.1 varnish
X-Cache: HIT
X-Cache-Hits: 0
X-Served-By: cache-fra1228-FRA
X-Timer: S1492621366.171241,VS0,VE0

As you can see, the content-length is 0 bytes, so the error message "unexpected end of input" would make sense ;-)

The redirect URL (https://xkcd.com/614/info.0.json) as well as the second URL return proper JSON with a 200 status code.

All 4 comments

Can you make sure that the HTTP client is following redirections? I get the following output with httpie;

$ http http://xkcd.com/614/info.0.json
HTTP/1.1 301 Moved Permanently
Accept-Ranges: bytes
Connection: close
Content-Length: 0
Date: Wed, 19 Apr 2017 17:02:46 GMT
Location: https://xkcd.com/614/info.0.json
Retry-After: 0
Server: Varnish
Via: 1.1 varnish
X-Cache: HIT
X-Cache-Hits: 0
X-Served-By: cache-fra1228-FRA
X-Timer: S1492621366.171241,VS0,VE0

As you can see, the content-length is 0 bytes, so the error message "unexpected end of input" would make sense ;-)

The redirect URL (https://xkcd.com/614/info.0.json) as well as the second URL return proper JSON with a 200 status code.

OK, I know, thank you very much!

Was that the issue?

Nothing~

Was this page helpful?
0 / 5 - 0 ratings

Related issues

garethsb-sony picture garethsb-sony  路  4Comments

MariaRamos89 picture MariaRamos89  路  4Comments

jmlemetayer picture jmlemetayer  路  3Comments

zhishupp picture zhishupp  路  4Comments

zkelo picture zkelo  路  3Comments