Skip to content

Commit 7138a18

Browse files
authored
Merge pull request #40 from libcpr/feature/disable_accept_encoding_header
Docs for disabling the 'Accept-Encoding' header
2 parents f413908 + 35210f5 commit 7138a18

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

advanced-usage.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,57 @@ Response response = session.Get();
283283
```
284284
{% endraw %}
285285
286+
### Defaults
287+
288+
To allow `cpr` and therefore `libcurl` decide which compressions are accepted, or just to enable all supported compression methods, pass an empty list to `cpr::AcceptEncoding`.
289+
**This is also the default behavior if nothing else gets configured.**
290+
291+
{% raw %}
292+
```c++
293+
// An empty list of accepted encodings in combination with a direct request without any state
294+
cpr::Response r = cpr::Get(cpr::Url{"http://www.httpbin.org/get"},
295+
cpr::AcceptEncoding{});
296+
297+
// An empty list of accepted encodings in combination with a stateful cpr::Session object
298+
session.SetAcceptEncoding(cpr::AcceptEncoding{});
299+
```
300+
{% endraw %}
301+
302+
### Disabling the `Accept-Encoding` Header
303+
304+
By default `cpr` and therefore `libcurl` will always include an `Accept-Encoding` header. To disable this behavior one can simply pass the `cpr::AcceptEncodingMethods::disabled` or `"disabled"` directly as a `std::string` to `cpr::AcceptEncoding`.
305+
306+
> ⚠️ **WARNING**<br>
307+
> Including `cpr::AcceptEncodingMethods::disabled` or `"disabled"` does not allow any other values/encodings to be passed to `cpr::AcceptEncoding`!<br>
308+
> If you ignore this a `std::invalid_argument` exception wil be thrown during session establishment.
309+
310+
{% raw %}
311+
```c++
312+
cpr::Session session;
313+
session.SetUrl("https://example.com");
314+
session.SetAcceptEncoding({AcceptEncodingMethods::disabled}); // Disable setting the `Accept-Encoding` header
315+
Response response = session.Get();
316+
```
317+
{% endraw %}
318+
319+
{% raw %}
320+
```c++
321+
cpr::Session session;
322+
session.SetUrl("https://example.com");
323+
session.SetAcceptEncoding({"disabled"}); // Disable setting the `Accept-Encoding` header
324+
Response response = session.Get();
325+
```
326+
{% endraw %}
327+
328+
{% raw %}
329+
```c++
330+
cpr::Session session;
331+
session.SetUrl("https://example.com");
332+
session.SetAcceptEncoding({AcceptEncodingMethods::disabled, AcceptEncodingMethods::deflate});
333+
Response response = session.Get(); // An exception of type `std::invalid_argument` will be thrown here since multiple values are passed to `AcceptEncoding` where one of them is `disabled`
334+
```
335+
{% endraw %}
336+
286337
For more information, please refer to [HTTP compression - Wikipedia](https://en.wikipedia.org/wiki/HTTP_compression) and [CURLOPT_ACCEPT_ENCODING](https://curl.se/libcurl/c/CURLOPT_ACCEPT_ENCODING.html).
287338
288339
## Large Responses

0 commit comments

Comments
 (0)