From 8e7acf6d40a886ab423bf66cdd8ca0f312511fad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20M=C3=BCller?= <17151282+simue@users.noreply.github.com> Date: Mon, 17 Feb 2025 11:13:42 +0100 Subject: [PATCH 1/4] draft session RemoveContent(), GetHeader() --- advanced-usage.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/advanced-usage.md b/advanced-usage.md index f6c0bef..ef576a3 100644 --- a/advanced-usage.md +++ b/advanced-usage.md @@ -194,6 +194,26 @@ std::cout << new_r.url << std::endl; // Prints http://www.httpbin.org/get?key ``` {% endraw %} +`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 be deleted manually: + +{% raw %} + +cpr::Url getUrl = cpr::Url{"http://www.httpbin.org/get"}; +cpr::Url postUrl = cpr::Url{"http://www.httpbin.org/post"}; +cpr::Session session; +session.SetUrl(url); +session.SetHeader(Header{{"My-Custom-Header", "hello"},{"Content-Type", "application/json"}}); +session.SetBody(Body{"x=5"}); + +cpr::Response r = session.Post(); // Equivalent to cpr::Post(url, body); // MISSING HEADER HERE -> whatÅ› the syntax? + +session.RemoveContent(); +auto& headers = session.GetHeader(); +headers.erase("content-type"); // headers keys are case-insensitive +cpr::Response new_r = session.Get(); // Equivalent to cpr::Get(getUrl); + +{% endraw %} + `Session` also allows you to get the full request URL before a request is actually made: {% raw %} From 0b90a88b2b48393fe0994fda5f7a0be005f6d332 Mon Sep 17 00:00:00 2001 From: Simon Mueller Date: Tue, 18 Feb 2025 14:00:47 +0100 Subject: [PATCH 2/4] working minimal example for RemoveContent() and deleting headers --- advanced-usage.md | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/advanced-usage.md b/advanced-usage.md index ef576a3..dc8833a 100644 --- a/advanced-usage.md +++ b/advanced-usage.md @@ -194,24 +194,29 @@ std::cout << new_r.url << std::endl; // Prints http://www.httpbin.org/get?key ``` {% endraw %} -`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 be deleted manually: +`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: {% raw %} - +```c++ cpr::Url getUrl = cpr::Url{"http://www.httpbin.org/get"}; cpr::Url postUrl = cpr::Url{"http://www.httpbin.org/post"}; cpr::Session session; -session.SetUrl(url); -session.SetHeader(Header{{"My-Custom-Header", "hello"},{"Content-Type", "application/json"}}); +session.SetUrl(postUrl); +session.SetHeader(Header{{"My-Custom-Header", "hello"}, {"Content-Type", "application/json"}}); session.SetBody(Body{"x=5"}); -cpr::Response r = session.Post(); // Equivalent to cpr::Post(url, body); // MISSING HEADER HERE -> whatÅ› the syntax? - -session.RemoveContent(); -auto& headers = session.GetHeader(); -headers.erase("content-type"); // headers keys are case-insensitive -cpr::Response new_r = session.Get(); // Equivalent to cpr::Get(getUrl); +cpr::Response postResponse = session.Post(); +std::cout << postResponse.text << std::endl; +// [...] "headers": " My-Custom-Header": " hello", "Content-Type": "application/json" [...] "data": "x=5" +session.RemoveContent(); // don't send a body in next request +auto& headers = session.GetHeader(); // also don't send unnecessary headers +headers.erase("content-type"); // headers interface is case-insensitive +session.SetUrl(getUrl); +cpr::Response getResponse = session.Get(); // Equivalent to cpr::Get(getUrl); +std::cout << getResponse.text << std::endl; +// [...] "headers": " My-Custom-Header": " hello", [...] /* no data */ +``` {% endraw %} `Session` also allows you to get the full request URL before a request is actually made: From f95cbef4cfccfbbf7722ff29b2615ab7eb5d132d Mon Sep 17 00:00:00 2001 From: simue <17151282+simue@users.noreply.github.com> Date: Tue, 18 Feb 2025 21:02:50 +0100 Subject: [PATCH 3/4] Update advanced-usage.md Co-authored-by: Fabian Sauter --- advanced-usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-usage.md b/advanced-usage.md index dc8833a..dc7e26d 100644 --- a/advanced-usage.md +++ b/advanced-usage.md @@ -194,7 +194,7 @@ std::cout << new_r.url << std::endl; // Prints http://www.httpbin.org/get?key ``` {% endraw %} -`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: +`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: {% raw %} ```c++ From b648a94b49601e367006afb918d135ef27183133 Mon Sep 17 00:00:00 2001 From: simue <17151282+simue@users.noreply.github.com> Date: Tue, 18 Feb 2025 21:03:07 +0100 Subject: [PATCH 4/4] Update advanced-usage.md Co-authored-by: Fabian Sauter --- advanced-usage.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/advanced-usage.md b/advanced-usage.md index dc7e26d..f80b63a 100644 --- a/advanced-usage.md +++ b/advanced-usage.md @@ -213,7 +213,7 @@ session.RemoveContent(); // don't send a body in next request auto& headers = session.GetHeader(); // also don't send unnecessary headers headers.erase("content-type"); // headers interface is case-insensitive session.SetUrl(getUrl); -cpr::Response getResponse = session.Get(); // Equivalent to cpr::Get(getUrl); +cpr::Response getResponse = session.Get(); // equivalent to cpr::Get(getUrl); std::cout << getResponse.text << std::endl; // [...] "headers": " My-Custom-Header": " hello", [...] /* no data */ ```