Merge pull request #19 from BlackMATov/dev

Dev
This commit is contained in:
2019-07-15 17:36:01 +07:00
committed by GitHub
4 changed files with 49 additions and 3 deletions

View File

@@ -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

View File

@@ -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};
};
}

View File

@@ -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_t>(http_code));
response_ = response(last_url, static_cast<http_code_t>(http_code));
response_.content = std::move(response_content_);
response_.headers = std::move(response_headers_);
response_.uploader = std::move(breq_.uploader());

View File

@@ -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")