Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unexpected end of input (not same as one before) #974

Closed
DragonOsman opened this issue Feb 15, 2018 · 4 comments
Closed

Unexpected end of input (not same as one before) #974

DragonOsman opened this issue Feb 15, 2018 · 4 comments

Comments

@DragonOsman
Copy link

DragonOsman commented Feb 15, 2018

As the title says, my issue is an "unexpected end of input" error. I have a HTTP web server I'm writing using an example from the Boost.Beast library, and in it I want to send a request to a currency API that gives back its response as JSON data. I'm trying to use this library for that.

I don't know what exactly caused the problem, but I can provide a link to a Gist with all of my code here at least: https://gist.github.com/DragonOsman/9a7d4ee116efc575dece7d6de46bd31d . The HTML and JavaScript files provided are also part of the project and may be needed to reproduce the issue.

The expected behavior for the JSON part is that I should get back a conversion rate as JSON data that would be stored in my cache storage.

The actual behavior is that I have a message that says there's an unexpected input error, and it seems like the response body I was expecting isn't there. There are other problems in the program itself, but I don't think I should talk about them here since this is about the JSON library.

The compiler I'm using is MSVC, latest version. Visual Studio 2017 IDE. OS is Windows 10.

This is not a bug report, just an issue I'm having that I thought I should ask about. Any help would be appreciated. Thanks in advance.

Edit to add: I forgot to say this earlier; I used ParcelJS to build my front-end code to make the country-currency-map stuff work. It runs its own server, though, so I had to close that and then run the application which has its own server.

@DragonOsman
Copy link
Author

DragonOsman commented Feb 16, 2018

I'm making this post to add something I'd forgotten earlier:
I used ParcelJS to build my front-end code to make the country-currency-map stuff work. It runs its own server, though, so I had to close that and then run the application which has its own server.

@nlohmann
Copy link
Owner

nlohmann commented Feb 17, 2018

The error message means that the text you passed to the parser is invalid JSON. If you use the latest version of the library, the error message will be more detailed (e.g. parse error at 2: unexpected end of input; expected string literal) and also contains the byte position of the error (in the example above: 2).

In your linked example, I assume the error stems from line

found = m_cache.insert_or_assign(found, query_data, std::make_pair(std::chrono::steady_clock::now(), json::parse(res.body())));

Could you provide the content of res.body()? I assume it is empty or any other non-valid JSON text.

@nlohmann nlohmann added the state: needs more info the author of the issue needs to provide more details label Feb 17, 2018
@DragonOsman
Copy link
Author

DragonOsman commented Feb 17, 2018

I believe that res.body() is an empty string. As you can see in the program output I gave above, the space in front of the message body received is: part has nothing in it. I tried to print res.body() there.

This code:

                        std::string host{ "openexchangerates.org" }, api_endpoint{ "/api/latest.json" }, base{ "USD" },
				app_id{ "MY_APP_ID" }, symbol{ query_data }, prettyprint{ "true" }, show_alternative{ "false" },
				target = api_endpoint + "?app_id=" + app_id + "&base=" + base +
				"&symbols=" + symbol + "&prettyprint=" + prettyprint + "&show_alternative=" + show_alternative,
				port{ "443" };
			int version = 11;

			// This object and the TCP socket perform our IO
			tcp::resolver resolver{ m_ioc };

			// Look up the domain name
			const auto results = resolver.resolve(host, port);

			// Make the connection on the IP address we get from a lookup
			boost::asio::connect(m_socket, results.begin(), results.end());

			// Set up an HTTP GET request message
			http::request<http::string_body> req{ http::verb::get, target, version };
			req.set(http::field::host, host);
			req.set(http::field::user_agent, BOOST_BEAST_VERSION_STRING);

			// Send the HTTP request to the remote host
			http::write(m_socket, req);

is where I'm trying to construct the host and port strings, get the IP address, and send the request to the remote host. I wonder if the API I'm sending the request to expects a JSON request. If it doesn't, then I don't understand what's wrong since I'm sure I gave it the correct URL.

Edit: This is the URL I'm using to query the API: https://openexchangerates.org/api/latest.json?app_id=MY_APP_ID&base=USD&symbols=PKR&prettyprint=true&show_alternative=false.

@nlohmann
Copy link
Owner

Well then we know why there is an error message. I guess there is nothing wrong with the library at this point.

@nlohmann nlohmann removed the state: needs more info the author of the issue needs to provide more details label Feb 17, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants