Reputation: 23
In yew 0.2 Request::get
worked fine but now yew::services
doesn't exist, how can I make a request to /api/ping
? do I need javascript?
Upvotes: 0
Views: 2694
Reputation: 580
Yew uses web-sys
to access browser specific APIs. The fetch
API can be called from rust by following the guide from Yew and web-sys
.
Upvotes: 0
Reputation: 178
You can also use gloo-net
as suggested by the official yew [tutorial](https://yew.rs/docs/tutorial#:~:text=Fetching%20data%20(using,the%20following%20crates%3A)
use gloo_net::http::Request;
Request::get(URL)
.send()
.await
.unwrap();
Upvotes: 0
Reputation: 33420
You could try using reqwasm.
For reqwasm 0.4.0:
use reqwasm::http::Request;
Request::get(url)
.send()
.await
.unwrap();
And/or you could use web_sys::{Request, RequestInit}
and wasm_bindgen_futures::JsFuture
.
For web-sys 0.3.55 and wasm-bindgen-futures 0.4.28:
let mut opts = RequestInit::new();
opts.method("GET");
let request = Request::new_with_str_and_init(url, &opts)?;
let window = web_sys::window().unwrap();
JsFuture::from(window.fetch_with_request(&request)).await?;
Upvotes: 2