https://github.com/sunng87/erguotou
high-level http client utilities for hyper
https://github.com/sunng87/erguotou
Last synced: 9 months ago
JSON representation
high-level http client utilities for hyper
- Host: GitHub
- URL: https://github.com/sunng87/erguotou
- Owner: sunng87
- Created: 2015-06-07T12:58:28.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2015-06-26T15:14:08.000Z (over 10 years ago)
- Last Synced: 2024-10-06T04:42:39.716Z (over 1 year ago)
- Language: Rust
- Homepage:
- Size: 148 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Erguotou
[](https://travis-ci.org/sunng87/erguotou)
[](./LICENSE)
[](https://crates.io/crates/erguotou)
Erguotou (the library) is a set of utilities for writing http client
with [hyper](https://github.com/hyperium/hyper).
[Erguotou](http://en.wikipedia.org/wiki/Erguotou) (the name) is a kind of Chinese wine (Bai-jiu).
This library is working in progress. Any suggestion are welcome on API
design.
## Cargo
```toml
[dependencies]
erguotou = "*"
```
## Usage
### JsonRPC
Demo code
```rust
use std::collections::BTreeMap;
use hyper::client::Client;
use rustc_serialize::json::ToJson;
use erguotou::json::{JsonParam, JsonRPC};
// the data you are going to send
// it can be anything of rustc_serialize::json::ToJson
let mut data = BTreeMap::new();
data.insert("hello".to_owned(), "world".to_owned());
// the hyper client
let client = Client::new();
// construct JsonParam from data
let mut json_param: JsonParam = JsonParam::from(&data as &ToJson);
// the hyper request builder calls, no need for body() and header()
// a single json() call to fill them all
let mut resp = client.post("http://localhost:8080")
.json(&mut json_param).send().unwrap();
```
### Form Post
Demo code
```rust
use std::collections::BTreeMap;
use hyper::client::Client;
use erguotou::form::{ToForm, FormParam, FormBody};
// the data you are going to send
// it can be anything implements ToForm.
// By default, we have BTreeMap built-in for ToForm
let mut data = BTreeMap::new();
data.insert("name".to_owned(), "Ning Sun".to_owned());
// the hyper client
let client = Client::new();
// construct JsonParam from data
let mut form_data: FormParam = FormParam::from(&data as &ToForm);
// the hyper request builder calls, no need for body() and header()
// a single form() call to fill them all
let mut resp = client.post("http://localhost:8080")
.form(&mut form_data).send().unwrap();
```
## License
MIT