简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM