Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive POST request content in Poco?

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 ?

like image 420
Abhinav Avatar asked Nov 27 '15 12:11

Abhinav


2 Answers

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

like image 82
Ignacio Avatar answered Sep 16 '22 23:09

Ignacio


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.

like image 37
Alex Avatar answered Sep 17 '22 23:09

Alex