Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 392
Jasurbek Nabijonov Avatar asked Mar 18 '26 20:03

Jasurbek Nabijonov


1 Answers

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.

like image 124
Jasurbek Nabijonov Avatar answered Mar 21 '26 11:03

Jasurbek Nabijonov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!