How to receive POST request content in Poco?
Asked Answered
H

4

6

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 ?

Himmler answered 27/11, 2015 at 12:35 Comment(0)
D
4

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.

Dreddy answered 28/11, 2015 at 4:41 Comment(2)
thanks for your response. But my question was not about how to read from string or stringbuffer, it was about poco library usage and is there any other way of getting post contents without reading full stream or is there any API which Poco provides to read POSTed contents.Himmler
Your questions are not very clear. Since the content is obviously in the stream, then the only way to get it out is to extract it from the stream. Other options exist, but do not involve Poco HTTP framework - you can create your own TCP server using Poco ServerSocket, parse headers, etc...Dreddy
B
4

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

Baccate answered 21/1, 2016 at 12:18 Comment(0)
B
2

Alex's answer in modern C++ (tested with C++17) with std::string:

auto & stream = hreq.stream();
const size_t len = hreq.getContentLength();
std::string buffer(len, 0);
stream.read(buffer.data(), len);
Brina answered 30/3, 2020 at 14:6 Comment(0)
A
1
 std::string recv_string;
 Poco::StreamCopier::copyToString(req.stream(), recv_string);
 std::cout << recv_string << std::endl;

Use above snippet like this.

void SRequestHandler::handleRequest(Poco::Net::HTTPServerRequest& hreq, Poco::Net::HTTPServerResponse& resp){
std::string recv_string;
Poco::StreamCopier::copyToString(req.stream(), recv_string);
std::cout << recv_string << std::endl;

Use this snippet and import poco streamcopier packages.

and import the modules correctly.

cheer

Aixlachapelle answered 21/6, 2022 at 9:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.