The `Get()` method is nothing but a shortcut of `Request()`. Using `Request()` directly is more complicated:
The `Get()` method is nothing but a shortcut of `Request()`. Using `Request()` directly is more complicated:
```cpp
```cpp
auto r = session.Request(webcc::RequestBuilder{}.
auto r = session.Request(webcc::RequestBuilder{}.Get("http://httpbin.org/get")());
Get("http://httpbin.org/get")
());
```
```
As you can see, a helper class named `RequestBuilder` is used to chain the parameters and finally build (don't miss the `()` operator) a request object.
As you can see, a helper class named `RequestBuilder` is used to chain the parameters and finally build a request object. Please pay attention to the `()` operator.
Both the shortcut and `Request()` accept URL query parameters:
Both the shortcut and `Request()` accept URL query parameters:
*NOTE: The HTTPS/SSL support requires the build option `WEBCC_ENABLE_SSL` to be enabled.*
*NOTE: The HTTPS/SSL support requires the build option `WEBCC_ENABLE_SSL` to be enabled.*
Listing GitHub public events is not a big deal:
Listing GitHub public events is not a big deal:
```cpp
```cpp
auto r = session.Get("https://api.github.com/events");
auto r = session.Get("https://api.github.com/events");
```
```
You can then parse `r->data()` to JSON object with your favorite JSON library. My choice for the examples is [jsoncpp](https://github.com/open-source-parsers/jsoncpp). But the library itself doesn't understand JSON nor require one. It's up to you to choose the most appropriate JSON library.
## Server API Examples
You can then parse `r->data()` to JSON object with your favorite JSON library. My choice for the examples is [jsoncpp](https://github.com/open-source-parsers/jsoncpp). But Webcc itself doesn't understand JSON nor require one. It's up to you to choose the most appropriate JSON library.
The shortcuts (`Get()`, `Post()`, etc.) are easier to use but `RequestBuilder` is more powerful. It provides a lot of functions for you to customize the request. Let's see more examples.
In order to list the followers of an authorized GitHub user, you need either **Basic Authorization**:
```cpp
session.Request(webcc::RequestBuilder{}.
Get("https://api.github.com/user/followers").
AuthBasic(login, password) // Should be replaced by real login and password
());
```
Or **Token Authorization**:
```cpp
session.Request(webcc::RequestBuilder{}.
Get("https://api.github.com/user/followers").
AuthToken(token) // Should be replaced by a valid token
());
```
Though **Keep-Alive** (i.e., persistent connection) is a good feature and enabled by default, you can turn it off:
```cpp
auto r = session.Request(webcc::RequestBuilder{}.
Get("http://httpbin.org/get").
KeepAlive(false) // No Keep-Alive
());
```
The API for other HTTP requests is no different from GET.
POST request needs a body which is normally a JSON string for REST API. Let's post a small UTF-8 encoded JSON string:
```cpp
session.Request(webcc::RequestBuilder{}.
Post("http://httpbin.org/post").
Body("{'name'='Adam', 'age'=20}").
Json().Utf8()
());
```
Webcc has the ability to stream large response data to a file. This is especially useful when downloading files.
```cpp
auto r = session.Request(webcc::RequestBuilder{}.
Get("http://httpbin.org/image/jpeg")(),
true); // !!!
// Move the streamed file to your destination.
file_body->Move("./wolf.jpeg");
```
Please check the [examples](https://github.com/sprinfall/webcc/tree/master/examples/) for more information.