Search code examples
c++jsonhttppoco-libraries

Poco HTTPClientSession adding headers to HTTPRequest


Try to send POST method to server using JSON. But, server also requires model, platform and platform version as headers to request. How can I add these headers to HTTPRequest. At Postman, I can add it in Headers tab. Eg. Model: Redmi 4 Platform: android. Feel free to edit to make it clear to others. Below there is my code who HTTPRequest creates:

Poco::JSON::Object obj;
obj.set("login", "log123");

obj.set("password","pas123");

Poco::Net::HTTPClientSession session("http://hostAddress", 
    80); //giving host name and port number
Poco::Net::HTTPRequest request(Poco::Net::HTTPRequest::HTTP_POST,
        "http://requestAddress","1.1"); //initializing request body
Poco::Net::HTTPResponse response;


std::stringstream ss;
obj.stringify(ss);

request.setContentType("application/json");
request.setContentLength(ss.str().length());

std::ostream& oStream = session.sendRequest(request);// sends request, returns open stream

obj.stringify(oStream);

std::istream& iStream = session.receiveResponse(response);

I tried to find some information at https://pocoproject.org/docs/Poco.Net.HTTPRequest.html. https://pocoproject.org/docs/Poco.Net.HTTPMessage.html. But without results.


Solution

  • There is one solution. It can help to others.

    request.add("string:key","string:value") //In order to add headers to request. 
    

    Eg:

    request.add("X-Make","Xiaomi");
    request.add("X-Model","Redmi 4");
    request.add("X-Platform","android");
    request.add("X-Platform-Version","6.0.1");
    

    It works for me.