Skip to content

Add a complete simple use case for how to download a page and use its contents for the client #532

Closed
@unfo

Description

@unfo

The current documentation is complete nor very beginner friendly:

let mut client = Client::new();

let res = client.get("http://example.domain").send().unwrap();
assert_eq!(res.status, hyper::Ok);

Things that should be added:

  • What are the full rust includes (extern crate / use lines) that are needed
  • How to read the contents and possibly just print it

For example if you just add

extern crate hyper;
use hyper::*;

And then try to read the body with this additional code to the example:

    let mut contents = String::new();
    res.read_to_string(&mut contents);

You will get the following error:

src/main.rs:11:22: 11:51 error: type `hyper::client::response::Response` does not implement any method in scope named `read_to_string`
src/main.rs:11  let body = try!(res.read_to_string(&mut contents));
                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Which is not clear for beginners since the documentation says that this function is implemented for the Response.

The missing code was:

use std::io::prelude::*;

or

use std::io::Read;

Please provide a complete tutorial on how to correctly download a page and use its contents in a idiomatic rust way. Extra points for good error handling also.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: documentation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions