Skip to content

Commit

Permalink
Merge pull request http-rs#93 from skade/master
Browse files Browse the repository at this point in the history
Fix readme examples
  • Loading branch information
yoshuawuyts committed Nov 8, 2019
2 parents 95bd3c8 + a49e5ef commit fa5b8d8
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,56 +62,68 @@ quick script, or a cross-platform SDK, Surf will make it work.
## Examples

```rust
#[runtime::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let mut res = surf::get("https://httpbin.org/get").await?;
dbg!(res.body_string().await?);
Ok(())
use async_std::task;

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
task::block_on(async {
let mut res = surf::get("https://httpbin.org/get").await?;
dbg!(res.body_string().await?);
Ok(())
});
}
```

It's also possible to skip the intermediate `Response`, and access the response
type directly.

```rust
#[runtime::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
dbg!(surf::get("https://httpbin.org/get").recv_string().await?);
Ok(())
use async_std::task;

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
task::block_on(async {
dbg!(surf::get("https://httpbin.org/get").recv_string().await?);
Ok(())
});
}
```

Both sending and receiving JSON is real easy too.

```rust
use async_std::task;
use serde::{Deserialize, Serialize};
#[runtime::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
#[derive(Deserialize, Serialize)]
struct Ip {
ip: String
}

let uri = "https://httpbin.org/post";
let data = &Ip { ip: "129.0.0.1".into() };
let res = surf::post(uri).body_json(data)?.await?;
assert_eq!(res.status(), 200);
task::block_on(async {
let uri = "https://httpbin.org/post";
let data = &Ip { ip: "129.0.0.1".into() };
let res = surf::post(uri).body_json(data)?.await?;
assert_eq!(res.status(), 200);

let uri = "https://api.ipify.org?format=json";
let Ip { ip } = surf::get(uri).recv_json().await?;
assert!(ip.len() > 10);
Ok(())
let uri = "https://api.ipify.org?format=json";
let Ip { ip } = surf::get(uri).recv_json().await?;
assert!(ip.len() > 10);
Ok(())
}
}
```

And even creating streaming proxies is no trouble at all.

```rust
#[runtime::main]
async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
let reader = surf::get("https://img.fyi/q6YvNqP").await?;
let res = surf::post("https://box.rs/upload").body(reader).await?;
Ok(())
use async_std::task;

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
task::block_on(async {
let reader = surf::get("https://img.fyi/q6YvNqP").await?;
let res = surf::post("https://box.rs/upload").body(reader).await?;
Ok(())
});
}
```

Expand Down

0 comments on commit fa5b8d8

Please sign in to comment.