I have a question about performing a HTTP POST request with a JSON body. I use the following code which works like a charm:
- Code: Select all
// Request
JSON::Object jsonRequest;
jsonRequest.set("firstName", firstName);
jsonRequest.set("lastName", lastName);
jsonRequest.set("accessToken", accessToken);
std::ostringstream jsonBody;
jsonRequest.stringify(jsonBody);
Net::HTTPRequest request(Net::HTTPRequest::HTTP_POST, path, Net::HTTPMessage::HTTP_1_1);
request.setContentType("application/json");
request.setContentLength(jsonBody.str().length());
session.sendRequest(request) << jsonBody.str();
I have 2 questions:
First, is it possible to send a POST request with a JSON body without specifying the content length header ? I tried appending 0x0d 0x0a to the end of the body to indicate termination but it didn't work... (I realize it's not a POCO specific question but a more general HTTP question)
Second, if it's not possible to avoid specifying the content-length header, is the above code correct? It seems a bit awkward to me.
Thanks,
BQ.





