mirror of
https://github.com/BlackMATov/curly.hpp.git
synced 2026-01-04 15:16:51 +07:00
add query parameters example
This commit is contained in:
15
README.md
15
README.md
@@ -30,6 +30,7 @@
|
|||||||
- Custom headers
|
- Custom headers
|
||||||
- Asynchronous requests
|
- Asynchronous requests
|
||||||
- Different types of timeouts
|
- Different types of timeouts
|
||||||
|
- URL encoded query parameters
|
||||||
- Completion and progress callbacks
|
- Completion and progress callbacks
|
||||||
- Custom uploading and downloading streams
|
- Custom uploading and downloading streams
|
||||||
- PUT, GET, HEAD, POST, PATCH, DELETE, OPTIONS methods
|
- 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
|
// 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
|
### Error Handling
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
|||||||
@@ -185,13 +185,18 @@ namespace curly_hpp
|
|||||||
response(const response&) = delete;
|
response(const response&) = delete;
|
||||||
response& operator=(const response&) = delete;
|
response& operator=(const response&) = delete;
|
||||||
|
|
||||||
explicit response(http_code_t c) noexcept
|
explicit response(std::string u, http_code_t c) noexcept
|
||||||
: http_code_(c) {}
|
: last_url_(u)
|
||||||
|
, http_code_(c) {}
|
||||||
|
|
||||||
bool is_http_error() const noexcept {
|
bool is_http_error() const noexcept {
|
||||||
return http_code_ >= 400u;
|
return http_code_ >= 400u;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const std::string& last_url() const noexcept {
|
||||||
|
return last_url_;
|
||||||
|
}
|
||||||
|
|
||||||
http_code_t http_code() const noexcept {
|
http_code_t http_code() const noexcept {
|
||||||
return http_code_;
|
return http_code_;
|
||||||
}
|
}
|
||||||
@@ -202,6 +207,7 @@ namespace curly_hpp
|
|||||||
downloader_uptr downloader;
|
downloader_uptr downloader;
|
||||||
progressor_uptr progressor;
|
progressor_uptr progressor;
|
||||||
private:
|
private:
|
||||||
|
std::string last_url_;
|
||||||
http_code_t http_code_{0u};
|
http_code_t http_code_{0u};
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -439,6 +439,17 @@ namespace curly_hpp
|
|||||||
return false;
|
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;
|
long http_code = 0;
|
||||||
if ( CURLE_OK != curl_easy_getinfo(
|
if ( CURLE_OK != curl_easy_getinfo(
|
||||||
curlh_.get(),
|
curlh_.get(),
|
||||||
@@ -451,7 +462,7 @@ namespace curly_hpp
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
response_ = response(static_cast<http_code_t>(http_code));
|
response_ = response(last_url, static_cast<http_code_t>(http_code));
|
||||||
response_.content = std::move(response_content_);
|
response_.content = std::move(response_content_);
|
||||||
response_.headers = std::move(response_headers_);
|
response_.headers = std::move(response_headers_);
|
||||||
response_.uploader = std::move(breq_.uploader());
|
response_.uploader = std::move(breq_.uploader());
|
||||||
|
|||||||
@@ -179,6 +179,7 @@ TEST_CASE("curly") {
|
|||||||
auto resp = req.take();
|
auto resp = req.take();
|
||||||
REQUIRE(req.status() == net::req_status::empty);
|
REQUIRE(req.status() == net::req_status::empty);
|
||||||
REQUIRE(resp.http_code() == 204u);
|
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();
|
auto req = net::request_builder("https://httpbin.org/delay/2").send();
|
||||||
@@ -484,6 +485,7 @@ TEST_CASE("curly") {
|
|||||||
.qparams(qparams.begin(), qparams.end())
|
.qparams(qparams.begin(), qparams.end())
|
||||||
.send();
|
.send();
|
||||||
const auto resp = req.take();
|
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());
|
const auto content_j = json_parse(resp.content.as_string_view());
|
||||||
REQUIRE(content_j["hello"] == "world");
|
REQUIRE(content_j["hello"] == "world");
|
||||||
REQUIRE(content_j["world"] == "hello");
|
REQUIRE(content_j["world"] == "hello");
|
||||||
@@ -971,6 +973,18 @@ TEST_CASE("curly_examples") {
|
|||||||
// Content Length: 389
|
// 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") {
|
SECTION("Error Handling") {
|
||||||
auto request = net::request_builder()
|
auto request = net::request_builder()
|
||||||
.url("http://unavailable.site.com")
|
.url("http://unavailable.site.com")
|
||||||
|
|||||||
Reference in New Issue
Block a user