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

web::json::json_exception when running an example #291

Open
wmunlai opened this Issue Nov 13, 2016 · 2 comments

Comments

Projects
None yet
3 participants
@wmunlai

wmunlai commented Nov 13, 2016

Hi,
I compiled the code below (obtained from Marius Bancila's Blog) in Visual Studio2013 and tried to run it. But I kept getting the exception:
Unhandled exception at 0x7593DB18 in restclient3.exe: Microsoft C++ exception: web::json::json_exception at memory location 0x0056EF80.

Wondering if someone could advise me on what is wrong. Thanks.

//#define _MS_WINDOWS

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <cpprest/json.h>

#include <iostream>



using namespace utility;
using namespace web;
using namespace web::http;
using namespace web::http::client;
using namespace concurrency::streams;

using namespace std;

void print_search_results(json::value const & value)
{
	if (!value.is_null())
	{
		auto response = value.at(L"responseData");
		auto results = response[L"results"];

		// iteration is supported not directly on json::value but on 
		// json::object and json::array. Use json::value::as_object() and json::value::as_array().
		for (auto const & o : results.as_array())
		{
			auto url = o.at(L"url");
			auto title = o.at(L"titleNoFormatting");

			std::wcout << title.as_string() << std::endl << url.as_string() << std::endl << std::endl;
		}
	}
}

void search_and_print(std::wstring const & searchTerm, int resultsCount)
{
	http_client client(U("https://ajax.googleapis.com/ajax/services/search/web"));

	// build the query parameters
	auto query = uri_builder()
		.append_query(L"q", searchTerm)
		.append_query(L"v", L"1.0")
		.append_query(L"rsz", resultsCount)
		.to_string();

	client
		// send the HTTP GET request asynchronous
		.request(methods::GET, query)
		// continue when the response is available
		.then([](http_response response) -> pplx::task<json::value>
	{
		// if the status is OK extract the body of the response into a JSON value
		// works only when the content type is application\json
		if (response.status_code() == status_codes::OK)
		{
			return response.extract_json();
		}

		// return an empty JSON value
		return pplx::task_from_result(json::value());
	})
		// continue when the JSON value is available
		.then([](pplx::task<json::value> previousTask)
	{
		// get the JSON value from the task and display content from it
		try
		{
			json::value const & v = previousTask.get();
			print_search_results(v);
		}
		catch (http_exception const & e)
		{
			std::wcout << e.what() << endl;
		}
	})
		.wait();
}

int main(int argc, char *args[])
{
	search_and_print(L"marius bancila", 5);
	while (1);
	return 0;
}
@ras0219-msft

This comment has been minimized.

Show comment
Hide comment
@ras0219-msft

ras0219-msft Nov 17, 2016

Contributor

The while(1); is certainly not needed; you're calling .wait() inside search_and_print, so it should already block until the request completes.

Have you tried wrapping the body of main() with a try/catch?

try { ... }
catch (const std::exception& e)
{ std::wcout << e.what() << endl; }

Clearly a json_exception is being thrown, and you're only catching http_exceptions inside the continuation.

Contributor

ras0219-msft commented Nov 17, 2016

The while(1); is certainly not needed; you're calling .wait() inside search_and_print, so it should already block until the request completes.

Have you tried wrapping the body of main() with a try/catch?

try { ... }
catch (const std::exception& e)
{ std::wcout << e.what() << endl; }

Clearly a json_exception is being thrown, and you're only catching http_exceptions inside the continuation.

@EliaPrincesaSirena

This comment has been minimized.

Show comment
Hide comment
@EliaPrincesaSirena

EliaPrincesaSirena Jul 31, 2018

Can you show an example of how it would be to replicate a GET with a json in the C ++ server part?
I tried this: But it sends random characters. And it has no sense because only the image binary is b64encoded :

`
std::ifstream InFile("Jellyfish.jpg", std::ifstream::binary);
std::vector data((std::istreambuf_iterator(InFile)), std::istreambuf_iterator());

	std::string Code = base64_encode((unsigned char*)&data[0], (unsigned int)data.size());
	//std::string Code(data.begin(), data.end());

	//build json string to convert
	//std::string MY_JSON = ("{\"image\" : \"");
	//MY_JSON += Code;
	//MY_JSON += ("\",\"type\" : \"file\"}");


	////set up json object
	//json::value obj;
	//obj.parse(utility::conversions::to_string_t(MY_JSON));

	json::value obj;
	wstring Codeaux(Code.begin(), Code.end());
	obj[U("image")] = json::value::string(Codeaux);;
	obj[U("type")] = json::value(U("file"));


	message.reply(status_codes::OK, obj)
		.then([](pplx::task<void> t4)
	{
		try {
			t4.get();
		}
		catch (...) {
			//
		}
	});`

EliaPrincesaSirena commented Jul 31, 2018

Can you show an example of how it would be to replicate a GET with a json in the C ++ server part?
I tried this: But it sends random characters. And it has no sense because only the image binary is b64encoded :

`
std::ifstream InFile("Jellyfish.jpg", std::ifstream::binary);
std::vector data((std::istreambuf_iterator(InFile)), std::istreambuf_iterator());

	std::string Code = base64_encode((unsigned char*)&data[0], (unsigned int)data.size());
	//std::string Code(data.begin(), data.end());

	//build json string to convert
	//std::string MY_JSON = ("{\"image\" : \"");
	//MY_JSON += Code;
	//MY_JSON += ("\",\"type\" : \"file\"}");


	////set up json object
	//json::value obj;
	//obj.parse(utility::conversions::to_string_t(MY_JSON));

	json::value obj;
	wstring Codeaux(Code.begin(), Code.end());
	obj[U("image")] = json::value::string(Codeaux);;
	obj[U("type")] = json::value(U("file"));


	message.reply(status_codes::OK, obj)
		.then([](pplx::task<void> t4)
	{
		try {
			t4.get();
		}
		catch (...) {
			//
		}
	});`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment