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?
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.
@niilz, did you see this https://github.com/SergioBenitez/Rocket/pull/1346
@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.