HTTP Request from inside WASM using emscripten_fetch() and EMSCRIPTEN_FETCH_SYNCHRONOUS

This example uses emscripten_fetch() in EMSCRIPTEN_FETCH_SYNCHRONOUS mode.

See the output in browser developer console.

Actual code:


emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS | EMSCRIPTEN_FETCH_REPLACE;
emscripten_fetch_t *fetch = emscripten_fetch(&attr, "https://httpbin.org/anything");
if (fetch->status == 200)
{
    char dat[fetch->numBytes + 1];
    memcpy(dat, fetch->data, fetch->numBytes);
    dat[fetch->numBytes] = '\0';
    printf("data: %s \n", dat);
}
emscripten_fetch_close(fetch);

Info