HttpRequest PUT content in poco library
Asked Answered
V

1

7

I want to send some data from a C++ application to a server using a HTTP PUT request. I am using poco library for networking in my application.

I am using this code snippet:

HTTPClientSession session(_uri.getHost(), _uri.getPort());
HTTPRequest req(HTTPRequest::HTTP_PUT, path, HTTPMessage::HTTP_1_1);

Where do I set the content (file) stream when I send the request? Can anyone show me an example using this library?

Veritable answered 9/4, 2012 at 17:28 Comment(0)
P
8

Quoting the online documentation for HTTPClientSession:

sendRequest() will return an output stream that can be used to send the request body. After you are done sending the request body, create a HTTPResponse object and pass it to receiveResponse().

The following snippet shows one way to use the output stream to read in a file:

try {
    Poco::Net::HTTPClientSession session("www.example.com");
    Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_PUT, "/foo");

    std::ostream& os = session.sendRequest(request);

    std::ifstream ifs("thefile.txt"); // missing: error handling
    Poco::StreamCopier::copyStream(ifs, os); // that's it :-)

    Poco::Net::HTTPResponse response;
    std::istream& rs = session.receiveResponse(response);
    // Do something with rs...

} catch (Poco::Exception& e) {
    std::cout << e.displayText() << std::endl;
}

Also, have a look at the slides for POCO Network programming. They show, among other things, how to use HTTPClientSession.

POCO documentation is terse and to the point; it is worthwhile to read it.

Parsee answered 22/5, 2012 at 16:48 Comment(1)
I have the same problem with a post request. My server doesn't get the body of request, when I try to sent it like you described.Muscat

© 2022 - 2024 — McMap. All rights reserved.