I've written a HTTP client in Poco which sends POST request to the HTTPServer Following is the snippet
Poco::Net::HTTPClientSession s("127.0.0.1", 9090);
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST, "/echo");
std::string body("rs=this is a random request body");
request.setContentLength(body.length());
s.sendRequest(request) << body;
The server receives the request, but following is the only way I could find to get the steam ( ie rs=this is a ....)
void SRequestHandler::handleRequest(Poco::Net::HTTPServerRequest& hreq, Poco::Net::HTTPServerResponse& resp){
std::istream &i = hreq.stream();
Poco::StreamCopier::copyStream(i, ss, hreq.getContentLength());
}
So the way left to get content sent by client is using the string. Is there a simpler/direct way of getting the content ?
You could try Poco/Net/HTMLForm:
Poco::Net::HTMLForm form(hreq, hreq.stream());
then you can use form.get("rs") or form["rs"] to get an std::string with the value.
https://pocoproject.org/docs/Poco.Net.HTMLForm.html
There are no strings involved in what you are currently doing - you are copying from istream to ostream. If you want to avoid that, you can read the contents of istream into a char array, something like this:
std::istream &i = hreq.stream();
int len = hreq.getContentLength();
char* buffer = new char[len];
i.read(buffer, len);
You should, of course, take care to avoid leaks.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With