-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
actix-web::client - how to get body of response? #536
Comments
Sorry, I guess I should have opened this here: Can it be moved, or do I need to close this and create a new one there? |
I found some code on the internet. It works like this: ...
client.request(request).and_then(|response| {
match response.status().as_u16() {
200 => match response.into_body().concat2().wait() {
Ok(data) => ... I forgot the type of But I think |
@hkcorac The syntax that you provided didn't work, the best I could come up with was the following, but I still don't get the body of the response:
It seems like the wait would need to be outside of the handler, but I couldn't get that to compile:
The following is the version from Cargo.toml:
Thanks for trying to help, |
Please learn a bit more about how to handle futures
Instead I'd suggest to do following:
The best way to work with futures is to change them |
For further questions feel free to visit our gitter |
@DoumanAsh I've tried to help because I faced the same problem like his before. Personally I think Issues section can be used for question like this. Again, it's just my opinion. It's your policy about Issues section, which I personally think that it is too hard. |
@hkcorac I'd like to encourage people to use gitter for questions and leave issues with bugs/enhancements We may need to add example of various client usages in examples repo though |
@crusty-dave I guess you might find the answer above. In my opinion, reddit is not a good place to ask for questions about coding snippets. Next time you might want to go straight to the project repository (and check the |
My repository, before I dropped |
Thanks @DoumanAsh , I understand. Your answer did help me too. |
Actix is a good place to start learning about futures IMO, use futures::future::lazy;
use actix_rt::System;
use actix_web::client::Client;
fn main() {
System::new("test").block_on(lazy(|| {
let client = Client::default();
client.get("https://www.rust-lang.org") // <- Create request builder
.header("User-Agent", "Actix-web")
.send() // <- Send http request
.map_err(|_| ())
.and_then(|mut response| { // <- server http response
println!("Response: {:?}", response);
response.body().and_then(move |bytes| {
let s = std::str::from_utf8(&bytes).expect("utf8 parse error)");
println!("html: {:?}", s);
// Ok(())
// Or return something else...
Ok(HttpResponse::Ok()
.content_type("text/html")
.body(format!("{}", s)))
})
.map_err(|_| ()) // <- Handle error properly
// .wait() // blocking...
})
}));
} |
This worked for me: let mut res = client
.get("http://example.com")
.await?;
let body = res.body().await?; // Bytes
let utf8 = std::str::from_utf8(body.as_ref()).unwrap(); // .as_ref converts Bytes to [u8] I believe it only works in my case because |
Im late to the party... But this works for me:
Not sure if this is the best practice, but is my first time with Rust. And if just in case, the toml has to be:
If you are sending the request to a secure url |
My issue is that I cannot find a good example of an actix-web client that shows how to retrieve the body of the response.
The following doesn't display the body.
What I get is the following:
That makes perfect sense, because when dealing with a socket, one would typically need to keep reading to get the next chunk of data. However, I thought that the .poll() would probably handle that.
However, I cannot find any examples on how to setup a future to be able to gather all of the chunks into the final response. Note that I posted on reddit 18 days ago and never received a response.
I suspect that the Response needs to be defined as a future, but I cannot seem to find a syntax that will work with the APIs that I am using.
I think I need to call something like the following, but how do I feed this to actix::run()? Do I have to convert it into an Actor message?
Any help would be appreciated. A pointer to a comprehensive example would be useful.
Thanks,
-Dave
The text was updated successfully, but these errors were encountered: