Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fetch api as backend on wasm for browser targets #1194

Open
tolstenko opened this issue Mar 7, 2025 · 0 comments
Open

Use fetch api as backend on wasm for browser targets #1194

tolstenko opened this issue Mar 7, 2025 · 0 comments

Comments

@tolstenko
Copy link

tolstenko commented Mar 7, 2025

Is your feature request related to a problem?

Some of us, like me, I use your library to set up cross-platform URL calls easily. It would make my life easier if I could only use your lib, and it would translate the request to fetchJavaScriptt calls if on the web assembly environment.

Possible Solution

Check if the EMSCRIPTEN, wasm, wasm32, or wasm64 flag is set. If so, use the fetch API from Javascript instead.

ex.:

#include <emscripten/fetch.h>
#include <stdio.h>

void on_success(emscripten_fetch_t *fetch) {
    printf("Received %llu bytes: %s\n", fetch->numBytes, fetch->data);
    emscripten_fetch_free(fetch);
}

void on_failure(emscripten_fetch_t *fetch) {
    printf("Fetch failed: HTTP status %d\n", fetch->status);
    emscripten_fetch_free(fetch);
}

int main() {
    emscripten_fetch_attr_t attr;
    emscripten_fetch_attr_init(&attr);
    attr.onsuccess = on_success;
    attr.onerror = on_failure;
    attr.requestMethod = "GET";

    printf("Fetching data...\n");
    emscripten_fetch(&attr, "https://api.example.com/data");

    return 0;
}

Alternatives

Internally, I am manually checking that in the way I told you above :-)

Additional Context

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant