When developing HTTP code, it's useful to be able to test it without needing to send real requests over the internet each time. There are a few ways we can do this:
Option 1: Use a local web server like Apache or Nginx to serve responses to HTTP requests sent to localhost or 127.0.0.1. We can configure it to return specific responses for testing.
Option 2: Use a tool like Postman or Insomnia to manually send requests to our code running locally. These let us craft any arbitrary request.
Option 3: Use an online service like httpbin.org that generates HTTP responses we can control via the URL path and query parameters. For example:
cpr::Url URL{
"https://httpbin.org/status/200"
};
cpr::Response r = cpr::Get(URL);
This will return a 200 OK response we can use for testing. We can change the URL to /status/404
to get a 404 Not Found response, etc.
Option 4: Create a mock or fake version of the cpr::Get
 function that returns hard-coded cpr::Response
 objects without making a network request:
cpr::Response Get(cpr::Url& url,
cpr::Parameters& params) {
cpr::Response r;
r.status_code = 200;
r.text = "Dummy response text";
return r;
}
Using techniques like these we can test our HTTP logic locally in a controlled way before deploying it to make real network requests.
Answers to questions are automatically generated and may not have been reviewed.
A detailed and practical tutorial for working with HTTP in modern C++ using the cpr
library.