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

Am I doing this wrong? Getting an empty string #1199

Closed
DragonOsman opened this issue Aug 17, 2018 · 1 comment
Closed

Am I doing this wrong? Getting an empty string #1199

DragonOsman opened this issue Aug 17, 2018 · 1 comment

Comments

@DragonOsman
Copy link

I switched to the "Basic" paid plan for currencylayer API for my currency conversion application and I tried to use their currency conversion endpoint. I got this JSON back from my query function as the response string:

{"info":{"quote":9.543697,"timestamp":1534517605},"privacy":"https://currencylayer.com/privacy","query":{"amount":10,"from":"USD","to":"MAD"},"result":95.43697,"success":true,"terms":"https://currencylayer.com/terms"}

I tried to access it like this:

double result = 0;

try
{
    result = j_res["result"].get<double>();
}
catch (const json::type_error &e)
{
    std::cerr << "Line 635: Error: " << e.what() << '\n';
    std::cerr << "j_res:\n" << j_res << '\n';
}

But I got an empty string back it seems. Anyone got any ideas?

Here's my code (Gist link): https://gist.github.com/DragonOsman/d00ea7ec3d49c128ad7789293f156a09 .

The actual JSON response I get back from the currency API is:

{"success":true,"terms":"https:\/\/currencylayer.com\/terms","privacy":"https:\/\/currencylayer.com\/privacy","query":{"from":"USD","to":"MAD","amount":10},"info":{"timestamp":1534517605,"quote":9.543697},"result":95.43697}

And the full HTTP response I get when making the GET request is:

HTTP/1.1 200 OK
Server: nginx
Date: Fri, 17 Aug 2018 15:08:01 GMT
Content-Type: application/json; Charset=UTF-8
Transfer-Encoding: chunked
X-Apilayer-Transaction-Id: e31e7ba5-604f-4635-9169-d7082460483a
Etag: becdcd38fad538e33145efdabc172d2c
Access-Control-Allow-Origin: *
X-Request-Time: 0.020

df
{"success":true,"terms":"https:\/\/currencylayer.com\/terms","privacy":"https:\/\/currencylayer.com\/privacy","query":{"from":"USD","to":"MAD","amount":10},"info":{"timestamp":1534517605,"quote":9.543697},"result":95.43697}
0

I hope someone can help me out here. Thanks in advance.

@DragonOsman
Copy link
Author

I fixed it by doing it like this:

http::response<http::string_body> res{
	std::piecewise_construct,
	std::make_tuple(std::move(conversion_result_str)),
	std::make_tuple(http::status::ok, req.version()) };
res.set(http::field::server, BOOST_BEAST_VERSION_STRING);
res.set(http::field::content_type, "text/plain");
res.content_length(res.body().size());
res.keep_alive(req.keep_alive());
return send(std::move(res));

The reason for it being empty was apparently the value of the Content-Length header not being set correctly. It was still 0.

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

1 participant