Skip to content

http_request returns empty string for JSON data #1713

@JustGuessing123

Description

@JustGuessing123

I call the following function with a string (input webCall) that requests earnings data for a particular stock. I can send a request that asks for the 4 prior reports and that works correctly. When I increase the request to want 5 or more prior reports the data that is returned in an empty string and the status code is ok (200). I have done the same request using a command line CURL command and it works so I know that the server I am requesting the data from is capable of returning whatever length of data I am requesting. Therefore, I am assuming I have not configured something correctly and the return value is getting rejected for some type of size limitation issue. I am very to new to using the the cpprestsdk so I am thinking my lack of knowledge on how to configure things is the issue, but I don't know what to do! FYI, I have not included the exact https request since it has an api_key code that I had to pay for. The code is as follows:

// Creates an HTTP request and prints the length of the response stream.
pplx::task HTTPStreamingAsync(std::string& returnString, int& returnCode, std::string webCall)
{
returnString = "";

const size_t newSize = 1000;
wchar_t wcstring[newSize];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcstring, newSize, webCall.c_str(), _TRUNCATE);
// Declare webservice to consume json data from it
http_client client(wcstring);

//Set parameters for request
http_request req(methods::GET);
req.headers().set_content_type(L"application/json");
//req.headers().set_content_type(L"application/json; charset=UTF-8");

// Make the request and asynchronously process the response. 
return client.request(req).then([&returnString, &returnCode](http_response response)
	{
		// Print the status code.
		returnCode = response.status_code();
		if (response.status_code() != status_codes::OK)
		{
			returnString = "";
			return pplx::task_from_result();
		}

		// process the response.
		istream bodyStream = response.body();
		unsigned int responseLength = (unsigned int)response.headers().content_length();

		std::cout << "response.status_code()=" << response.status_code() << "\n";
		std::cout << "responseLength=" << responseLength << "\n";

		container_buffer<std::string> inStringBuffer;
		return bodyStream.read(inStringBuffer, responseLength).then([inStringBuffer, &returnString](size_t bytesRead)
			{
				const std::string& text = inStringBuffer.collection();
				//std::cout << text + "\n";
				returnString = text;
			});
	});

}

Also, just to know, the request is for JSON data and I just want to get the raw string and I will parse it using a JSON parser that I have already built.

Any help or suggestions would be greatly appreciated!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions