reqwest icon indicating copy to clipboard operation
reqwest copied to clipboard

Can't perform any request from a browser WASM application

Open ufoscout opened this issue 4 years ago • 2 comments

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.

ufoscout avatar Jul 16 '21 10:07 ufoscout

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

t-cadet avatar Sep 17 '21 16:09 t-cadet

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.

andrewbanchich avatar Jan 14 '22 19:01 andrewbanchich

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.

cryptoquick avatar Oct 27 '22 06:10 cryptoquick