Brian3647
Brian3647

Reputation: 23

Do a web request in yew.rs v0.19

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

Answers (3)

Adam Comer
Adam Comer

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.

Yew web-sys Integration Docs

web-sys fetch Docs

Upvotes: 0

collinsmarra
collinsmarra

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

Sebastián Palma
Sebastián Palma

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

Related Questions