From 3a80cc486313a896732e0b53378c930f46b90901 Mon Sep 17 00:00:00 2001 From: BlackMATov Date: Mon, 15 Jul 2019 17:35:36 +0700 Subject: [PATCH] add query parameters example --- README.md | 15 +++++++++++++++ headers/curly.hpp/curly.hpp | 10 ++++++++-- sources/curly.hpp/curly.cpp | 13 ++++++++++++- untests/curly_tests.cpp | 14 ++++++++++++++ 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0c84ad7..0c69978 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,7 @@ - Custom headers - Asynchronous requests - Different types of timeouts +- URL encoded query parameters - Completion and progress callbacks - Custom uploading and downloading streams - PUT, GET, HEAD, POST, PATCH, DELETE, OPTIONS methods @@ -128,6 +129,20 @@ std::cout << "Content Length: " << response.headers["content-length"] << std::en // Content Length: 389 ``` +### Query Parameters + +```cpp +auto request = net::request_builder() + .url("http://httpbin.org/anything") + .qparam("hello", "world") + .send(); + +auto response = request.take(); +std::cout << "Last URL: " << response.last_url() << std::endl; + +// Last URL: http://httpbin.org/anything?hello=world +``` + ### Error Handling ```cpp diff --git a/headers/curly.hpp/curly.hpp b/headers/curly.hpp/curly.hpp index 5540a2b..3b55e40 100644 --- a/headers/curly.hpp/curly.hpp +++ b/headers/curly.hpp/curly.hpp @@ -185,13 +185,18 @@ namespace curly_hpp response(const response&) = delete; response& operator=(const response&) = delete; - explicit response(http_code_t c) noexcept - : http_code_(c) {} + explicit response(std::string u, http_code_t c) noexcept + : last_url_(u) + , http_code_(c) {} bool is_http_error() const noexcept { return http_code_ >= 400u; } + const std::string& last_url() const noexcept { + return last_url_; + } + http_code_t http_code() const noexcept { return http_code_; } @@ -202,6 +207,7 @@ namespace curly_hpp downloader_uptr downloader; progressor_uptr progressor; private: + std::string last_url_; http_code_t http_code_{0u}; }; } diff --git a/sources/curly.hpp/curly.cpp b/sources/curly.hpp/curly.cpp index f6a1aa6..8484ff8 100644 --- a/sources/curly.hpp/curly.cpp +++ b/sources/curly.hpp/curly.cpp @@ -439,6 +439,17 @@ namespace curly_hpp return false; } + char* last_url = nullptr; + if ( CURLE_OK != curl_easy_getinfo( + curlh_.get(), + CURLINFO_EFFECTIVE_URL, + &last_url) || !last_url ) + { + status_ = req_status::failed; + cvar_.notify_all(); + return false; + } + long http_code = 0; if ( CURLE_OK != curl_easy_getinfo( curlh_.get(), @@ -451,7 +462,7 @@ namespace curly_hpp } try { - response_ = response(static_cast(http_code)); + response_ = response(last_url, static_cast(http_code)); response_.content = std::move(response_content_); response_.headers = std::move(response_headers_); response_.uploader = std::move(breq_.uploader()); diff --git a/untests/curly_tests.cpp b/untests/curly_tests.cpp index 55e1873..e14c1b4 100644 --- a/untests/curly_tests.cpp +++ b/untests/curly_tests.cpp @@ -179,6 +179,7 @@ TEST_CASE("curly") { auto resp = req.take(); REQUIRE(req.status() == net::req_status::empty); REQUIRE(resp.http_code() == 204u); + REQUIRE(resp.last_url() == "https://httpbin.org/status/204"); } { auto req = net::request_builder("https://httpbin.org/delay/2").send(); @@ -484,6 +485,7 @@ TEST_CASE("curly") { .qparams(qparams.begin(), qparams.end()) .send(); const auto resp = req.take(); + REQUIRE(resp.last_url() == "https://httpbin.org/response-headers?hello=world&world=hello"); const auto content_j = json_parse(resp.content.as_string_view()); REQUIRE(content_j["hello"] == "world"); REQUIRE(content_j["world"] == "hello"); @@ -971,6 +973,18 @@ TEST_CASE("curly_examples") { // Content Length: 389 } + SECTION("Query Parameters") { + auto request = net::request_builder() + .url("http://httpbin.org/anything") + .qparam("hello", "world") + .send(); + + auto response = request.take(); + std::cout << "Last URL: " << response.last_url() << std::endl; + + // Last URL: http://httpbin.org/anything?hello=world + } + SECTION("Error Handling") { auto request = net::request_builder() .url("http://unavailable.site.com")