Skip to content

Commit 250c254

Browse files
authored
Merge pull request #55 from simue/feature/remove-content-remove-header
Documentation for Session::RemoveContent() and header removal
2 parents d181a28 + b648a94 commit 250c254

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

advanced-usage.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,31 @@ std::cout << new_r.url << std::endl; // Prints http://www.httpbin.org/get?key
194194
```
195195
{% endraw %}
196196

197+
`Session`'s state also includes any custom headers and content being sent. Once you performed a request both will be re-attached to any subsequent request. Resending the body can be prevented by a call to `RemoveContent()`, while headers can also be deleted manually:
198+
199+
{% raw %}
200+
```c++
201+
cpr::Url getUrl = cpr::Url{"http://www.httpbin.org/get"};
202+
cpr::Url postUrl = cpr::Url{"http://www.httpbin.org/post"};
203+
cpr::Session session;
204+
session.SetUrl(postUrl);
205+
session.SetHeader(Header{{"My-Custom-Header", "hello"}, {"Content-Type", "application/json"}});
206+
session.SetBody(Body{"x=5"});
207+
208+
cpr::Response postResponse = session.Post();
209+
std::cout << postResponse.text << std::endl;
210+
// [...] "headers": " My-Custom-Header": " hello", "Content-Type": "application/json" [...] "data": "x=5"
211+
212+
session.RemoveContent(); // don't send a body in next request
213+
auto& headers = session.GetHeader(); // also don't send unnecessary headers
214+
headers.erase("content-type"); // headers interface is case-insensitive
215+
session.SetUrl(getUrl);
216+
cpr::Response getResponse = session.Get(); // equivalent to cpr::Get(getUrl);
217+
std::cout << getResponse.text << std::endl;
218+
// [...] "headers": " My-Custom-Header": " hello", [...] /* no data */
219+
```
220+
{% endraw %}
221+
197222
`Session` also allows you to get the full request URL before a request is actually made:
198223
199224
{% raw %}

0 commit comments

Comments
 (0)