seanmonstar / reqwest

An easy and powerful Rust HTTP Client
https://docs.rs/reqwest
Apache License 2.0
9.69k stars 1.09k forks source link

Can't perform any request from a browser WASM application #1306

Open ufoscout opened 3 years ago

ufoscout commented 3 years ago

I am trying to use reqwest to call my backend from a Yew-based WASM frontend running in the browser.

HTTP calls to the backend are typically performed by a frontend application using relative URLs; anyway, this is not possible with reqwest as every attempt to send a call with a relative URL produces a RelativeUrlWithoutBase error.

This problem makes reqwest unsuitable for every rust frontend framework.

This issue is closely related to #988, but #988 is quite generic while this one aims for a specific solution for WASM in the browser.

t-cadet commented 2 years ago

I have the same issue using Yew frontend with Trunk proxy, is there a work around ?

andrewbanchich commented 2 years ago

I'm interested in the same thing. It sounds like #988 would solve this though, as the issue isn't specific to WASM.

I think a workaround would use client-side JS to get the current base URL and pass it into the WASM function, then use join to combine it with the relative URL.

cryptoquick commented 1 year ago

I think the best way to handle this is to make requests using the gloo_net API, which is a wrapper around the fetch API. It isn't isomorphic to native Rust however, so this can be a pain in applications where that matters. I usually solve for this by leveraging conditional compilation. Come to think of it, now that fetch is in NodeJS 18, I think it might work fine on the backend in that instance.