1

How do I make an http GET request with headers and possibly a body using c++ rest SDK? I'm able to make a GET request without headers / body as follows:

web::http::client::http_client client(U("https://host.com"));
pplx::task<http_response> response = client.request(methods::GET, U("/URL/"));
response.then(...)......

and a POST request works with headers and json body like this:

web::json::value body = web::json::value::object();
web::http::client::http_client client(U("Full URL"));
http_request req(methods::POST);
req.headers().add(U("..."), U("..."));
req.headers().add(U("..."), ...);
//construct body here
req.set_body(body);
auto response = client.request(req).then(...)......

This also works. But everytime I try a GET request and add the headers / body, I get an error with the reponse.

At first I thought it would just be a matter of using the POST method and changing methods::POST to methods::GET but this does not work.

Thanks in advance

EDIT: When just trying to add headers, this is what I originally thought would work:

web::http::client::http_client client(U("Full URL"));
http_request req(methods::GET);
req.headers().add(U("..."), U("..."));
req.headers().add(U("..."), ...);
auto response = client.request(req).then(...)......
James
  • 11
  • 2
  • It is unclear how it makes sense to have body/payload in a GET request? Perhaps such request is rejected. – Öö Tiib May 23 '23 at 13:25
  • GET requests can't have bodies, its not allowed by the HTTP standard https://datatracker.ietf.org/doc/html/rfc7231#section-4.3.1 – Alan Birtles May 23 '23 at 13:30
  • In that case, it appears the GET request requires just headers and no body. How would it be done with headers? – James May 23 '23 at 14:22
  • Please show a [mre], "doesn't work" isn't a great description of your problem. What i exactly is happening, what's the error message? – Alan Birtles May 24 '23 at 06:52

0 Answers0