Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
unfo opened this issue May 15, 2015 · 6 comments
Labels
A-docs Area: documentation.

Comments

@unfo
Copy link

unfo commented May 15, 2015

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.

@seanmonstar seanmonstar added the A-docs Area: documentation. label May 17, 2015
@brycefisher
Copy link

My vote here is to provide two client examples:

  • The existing, overly-simplistic client.rs (for quickly wrapping your around a simple example)
  • A new example that DOES something more interesting, like:
    • try to print your GPS coordinates from your IP address (using freegeoip.net etc)
    • print the an issue number, title, and url from this repo using the github api (would require parsing json)
    • scrape craigslist for a used guitar
    • find an interesting piece of trivia from an opengov API

@pyrossh
Copy link

pyrossh commented Feb 6, 2016

Yeah More docs please. I Couldn't even get google.com it gave me this error.

thread 'send_request' panicked at 'called `Result::unwrap()` on an `Err` value: Error { repr: Custom(Custom { kind: InvalidData, error: StringError("stream did not contain valid UTF-8") }) }', src/libcore/result.rs:741

@mfarrugi
Copy link

mfarrugi commented Apr 18, 2016

The front page readme / examples of this project are still broken, and also requires the Response to be mutable.

@seanmonstar
Copy link
Member

@mfarrugi broken? seemed there was an unused_mut warning, which has been fixed in b98662a, but I don't otherwise see them broken...

@mfarrugi
Copy link

I've got no idea what I thought was broken. Forgive me :)

@seanmonstar
Copy link
Member

Closing in favor of #805

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-docs Area: documentation.
Projects
None yet
Development

No branches or pull requests

5 participants