Rocket: Is there a way to make outgoing Requests? - reqwest-crate does not seem to work inside rocket

Created on 5 Jul 2020  路  5Comments  路  Source: SergioBenitez/Rocket

Background/Version

  1. Rocket-Version: 0.4.5
  2. I've looked through the docs, the website, issues and the approach on this blog-post.
  3. I think it could be mentioned under "Requests" in the documentation, where currently only incoming requests are mentioned. Which is of course as expected, rocket being a REST-Framework. But it would be great if there was a little note on that this is either not possible or maybe a hint on where to look for such functionality.

Question:

I've read on issue #250 that outbound requests are out of scope of rocket. Which is understandable. But there reqwest get's pointed at, as a possible solution (which is also used in the blog-post I've linked to above).
So I've tried to use reqwest (version 0.10.06) and can not find a way to make it work, once rocket is launched. Could it be that older versions of reqwest (the blog-post-version seems to use 0.9.3) were synchronous, where the current version is completely Futures based, which makes it incompatible with rocket?

It looks like rocket is about to support async await. Would that allow using asynchronous, outgoing requests using something like reqwest? If so, is it suggested to try out rocket's async-branch?
Or am I just missing something?

question

All 5 comments

i actually just built a prototype for something and this worked for me: reqwest has a feature called blocking.
note: this being a prototype the code is (intentionally - i don't want the prototype to morph into a real thing!) terrible and doesn't contain any error handling:

[dependencies]
rocket = "0.4.5"
reqwest = { version = "0.10.6", features = ["blocking"] }
#[get("/foo")]
fn foo() -> content::Json<String> {
    content::Json(
        reqwest::blocking::get("http://example.org/".as_str())
            .unwrap()
            .text()
            .unwrap(),
    )
}

i presume once async rocket is released this code will look completely different (use async reqwest) and much nicer/cleaner.

but i concur: once async rocket has been merged it'd be great if an example could be mentioned in the documentation since calling another (micro)service in a synchronous manner (usually via HTTP rather than a custom RPC protocol) is a general pattern which is often employed.

For now reqwest::blocking looks exactly like what I need. Thank you very much, @rursprung
And I am also very interested to see what the Future brings (pardon the pun).

I am closing this Issue, since the main part of my question has been answered. And I think async/await topics are already discussed in other issue-threads.

@igalic: it seems that this Client is only for testing and will only issue requests against the current rocket instance: https://github.com/SergioBenitez/Rocket/blob/async/core/lib/src/local/client.rs
or am i missing something?

Thanks @igalic for pointing that out. I had not really looked into it.
It seems like a great feature but from how I understand the docs it seems @rursprung is right and Client does not seem to provide outbound-request-functionality.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paulvt picture paulvt  路  4Comments

denysvitali picture denysvitali  路  3Comments

ndarilek picture ndarilek  路  3Comments

incker picture incker  路  3Comments

Perseus101 picture Perseus101  路  4Comments