mirror of
https://github.com/BlackMATov/curly.hpp.git
synced 2026-01-04 15:16:51 +07:00
add two examples to README
This commit is contained in:
77
README.md
77
README.md
@@ -31,7 +31,82 @@
|
|||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
> coming soon!
|
```cpp
|
||||||
|
#include <curly.hpp/curly.hpp>
|
||||||
|
namespace net = curly_hpp;
|
||||||
|
|
||||||
|
// creates and hold a separate thread for automatically update async requests
|
||||||
|
net::auto_performer performer;
|
||||||
|
|
||||||
|
// also, you can update requests manually from your favorite thread
|
||||||
|
net::perform();
|
||||||
|
```
|
||||||
|
|
||||||
|
### GET Requests
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// makes a GET request and async send it
|
||||||
|
auto request = net::request_builder()
|
||||||
|
.method(net::methods::get)
|
||||||
|
.url("http://www.httpbin.org/get")
|
||||||
|
.send();
|
||||||
|
|
||||||
|
// synchronous waits and get a response
|
||||||
|
auto response = request.get();
|
||||||
|
|
||||||
|
// prints results
|
||||||
|
std::cout << "Status code: " << response.code() << std::endl;
|
||||||
|
std::cout << "Content type: " << response.headers()["content-type"] << std::endl;
|
||||||
|
std::cout << "Body content: " << response.content().as_string_view() << std::endl;
|
||||||
|
|
||||||
|
// Status code: 200
|
||||||
|
// Content type: application/json
|
||||||
|
// Body content: {
|
||||||
|
// "args": {},
|
||||||
|
// "headers": {
|
||||||
|
// "Accept": "*/*",
|
||||||
|
// "Host": "www.httpbin.org",
|
||||||
|
// "User-Agent": "cURL/7.54.0"
|
||||||
|
// },
|
||||||
|
// "origin": "37.195.66.134, 37.195.66.134",
|
||||||
|
// "url": "https://www.httpbin.org/get"
|
||||||
|
// }
|
||||||
|
```
|
||||||
|
|
||||||
|
### POST Requests
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
auto request = net::request_builder()
|
||||||
|
.method(net::methods::post)
|
||||||
|
.url("http://www.httpbin.org/post")
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.content(R"({"hello" : "world"})")
|
||||||
|
.send();
|
||||||
|
|
||||||
|
auto response = request.get();
|
||||||
|
std::cout << "Status code: " << response.code() << std::endl;
|
||||||
|
std::cout << "Body content: " << response.content().as_string_view() << std::endl;
|
||||||
|
|
||||||
|
// Status code: 200
|
||||||
|
// Body content: {
|
||||||
|
// "args": {},
|
||||||
|
// "data": "{\"hello\" : \"world\"}",
|
||||||
|
// "files": {},
|
||||||
|
// "form": {},
|
||||||
|
// "headers": {
|
||||||
|
// "Accept": "*/*",
|
||||||
|
// "Content-Length": "19",
|
||||||
|
// "Content-Type": "application/json",
|
||||||
|
// "Host": "www.httpbin.org",
|
||||||
|
// "User-Agent": "cURL/7.54.0"
|
||||||
|
// },
|
||||||
|
// "json": {
|
||||||
|
// "hello": "world"
|
||||||
|
// },
|
||||||
|
// "origin": "37.195.66.134, 37.195.66.134",
|
||||||
|
// "url": "https://www.httpbin.org/post"
|
||||||
|
// }
|
||||||
|
```
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,7 @@ namespace
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("curly"){
|
TEST_CASE("curly") {
|
||||||
net::auto_performer performer;
|
net::auto_performer performer;
|
||||||
|
|
||||||
SECTION("wait") {
|
SECTION("wait") {
|
||||||
@@ -360,3 +360,69 @@ TEST_CASE("curly"){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("curly_examples") {
|
||||||
|
net::auto_performer performer;
|
||||||
|
|
||||||
|
SECTION("Get Requests") {
|
||||||
|
// makes a GET request and async send it
|
||||||
|
auto request = net::request_builder()
|
||||||
|
.method(net::methods::get)
|
||||||
|
.url("http://www.httpbin.org/get")
|
||||||
|
.send();
|
||||||
|
|
||||||
|
// synchronous waits and get a response
|
||||||
|
auto response = request.get();
|
||||||
|
|
||||||
|
// prints results
|
||||||
|
std::cout << "Status code: " << response.code() << std::endl;
|
||||||
|
std::cout << "Content type: " << response.headers()["content-type"] << std::endl;
|
||||||
|
std::cout << "Body content: " << response.content().as_string_view() << std::endl;
|
||||||
|
|
||||||
|
// Status code: 200
|
||||||
|
// Content type: application/json
|
||||||
|
// Body content: {
|
||||||
|
// "args": {},
|
||||||
|
// "headers": {
|
||||||
|
// "Accept": "*/*",
|
||||||
|
// "Host": "www.httpbin.org",
|
||||||
|
// "User-Agent": "cURL/7.54.0"
|
||||||
|
// },
|
||||||
|
// "origin": "37.195.66.134, 37.195.66.134",
|
||||||
|
// "url": "https://www.httpbin.org/get"
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("Post Requests") {
|
||||||
|
auto request = net::request_builder()
|
||||||
|
.method(net::methods::post)
|
||||||
|
.url("http://www.httpbin.org/post")
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.content(R"({"hello" : "world"})")
|
||||||
|
.send();
|
||||||
|
|
||||||
|
auto response = request.get();
|
||||||
|
std::cout << "Status code: " << response.code() << std::endl;
|
||||||
|
std::cout << "Body content: " << response.content().as_string_view() << std::endl;
|
||||||
|
|
||||||
|
// Status code: 200
|
||||||
|
// Body content: {
|
||||||
|
// "args": {},
|
||||||
|
// "data": "{\"hello\" : \"world\"}",
|
||||||
|
// "files": {},
|
||||||
|
// "form": {},
|
||||||
|
// "headers": {
|
||||||
|
// "Accept": "*/*",
|
||||||
|
// "Content-Length": "19",
|
||||||
|
// "Content-Type": "application/json",
|
||||||
|
// "Host": "www.httpbin.org",
|
||||||
|
// "User-Agent": "cURL/7.54.0"
|
||||||
|
// },
|
||||||
|
// "json": {
|
||||||
|
// "hello": "world"
|
||||||
|
// },
|
||||||
|
// "origin": "37.195.66.134, 37.195.66.134",
|
||||||
|
// "url": "https://www.httpbin.org/post"
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user