I'm calling a REST WS with a JSON payload to subscribe certains events. The server answer with HTTP-Code 201 and a field named Location in the HTTP-Header with the ID of the subscription.
As an example, in curl (-v) we get:
[...]
< HTTP/1.1 201 Created
< Connection: Keep-Alive
< Content-Length: 0
< Location: /v2/subscriptions/5ab386ad4bf6feec37ffe44d
[...]
In C++ using curlpp we want to retrieve that id looking at the response header. Now we have only the body response (in this case empty).
std::ostringstream response;
subRequest.setOpt(new curlpp::options::WriteStream(&response));
// Send request and get a result.
subRequest.perform();
cout << response.str() << endl;
How can we obtain the Location header's field (whose content in the example is "/v2/subscriptions/5ab386ad4bf6feec37ffe44d") in C++ using curlpp?
There are several values you can retrieve using the family of curlpp::infos::*::get
functions. For example the HTTP response code:
curlpp::infos::ResponseCode::get(subRequest)
See the Infos.hpp header for a complete list. When you need a value that is not available through one of these infos you can also choose to receive the headers separately from the body in a callback.
subRequest.setOpt(new curlpp::options::HeaderFunction(
[] (char* buffer, size_t size, size_t items) -> size_t {
std::string s(buffer, size * items); // buffer is not null terminated
std::cout << s;
return size * items;
}));