What's the proper way to use fsNetworkHttpRequestGet?

Hello. I wonder if anyone can help me. On MSFS 2024
I created a WASM package with the following code:

#include <MSFS/MSFS_Network.h>
#include <iostream>

void requestCB(FsNetworkRequestId requestId, int errorCode, void* userData)
{
    if (errorCode != 200) {
        printf("Failed with error code: %d", errorCode);
        return;
    }
    FsNetworkHttpRequestState state = fsNetworkHttpRequestGetState(requestId);
    if (state != FS_NETWORK_HTTP_REQUEST_STATE_DATA_READY)
    {
        printf("Failed to get data. Final state: %d", state);
        return;
    }
    unsigned long data_size = fsNetworkHttpRequestGetDataSize(requestId);
    unsigned char* data_p = fsNetworkHttpRequestGetData(requestId);
    std::string data(static_cast<char*>(static_cast<void*>(data_p)), data_size);
    printf("Result: %d with data: %s", errorCode, data.c_str());
}

void request()
{
    FsNetworkHttpRequestParam param;
    param.headerOptionsSize = 0;
    param.headerOptions = nullptr;
    param.data = nullptr;
    param.dataSize = 0;
    fsNetworkHttpRequestGet("http://localhost:3000/", &param, requestCB, nullptr);
}

I have a simple local express server running:

const express = require('express')
const app = express()
const port = 3000

app.get('/', (req, res) => {
    res.send('Hello World!')
})

app.listen(port, () => {
    console.log(`Example app listening on port ${port}`)
})

When I execute request my requestCB function never gets called.
This is the minimum viable test of fsNetworkHttpRequestGet I can come up with but it still doesn’t work.
I read the documentation but it’s doesn’t provide much help fsNetworkHttpRequestGet
Does anyone know what’s wrong here?

Only works with HTTPS :wink:

Which begs the question, why? HTTP is perfectly valid for talking to local apps on the same machine.

To be honest I suspected it because getting https://www.google.com/ does work.
But I never imagined it was the intended design. I thought it was more likely a bug.
Even the function name fsNetworkHttpRequestGet has the Http acronym and not Https. And the documentation says nothing about https.

I echo @tracernz , Why?
Isn’t my described setup of a WASM module requesting from a local server valid?

It is documented in the Network API doc (but not in the function’s doc).

Finally, note that only https requests are permitted by the API.

It indeed sucks because it prevents any kind of communication with a local server. Maybe that’s intentional, because of Xbox/PS?

Yep, it is a problem when creating local apps and is not worth the effort. I tried to use the Network API in my hobby project to communicate between my WASM module and my external EXE and therefor created a self signed certificate to be able to listen to https requests but that introduces a lot of security issues so I decided to use SimConnect instead.

I just tried setting up a self signed certificate but even that failed. Only valid certificates from certificate authorities seem to work.

Thanks for the suggestion @Swede001
I’ll try using SimConnect instead though I originally didn’t go for it because I couldn’t find a way to send events (button presses) from JS to an external SimConnect exe.

Lol the ability to use websockets and http requests from html/js is the most powerful API in the game by a huge margin. Without that we’d be c.1980’s multiplexing complex data in the sim into stacks of 64-bit L vars just to try and get strings or structured data types through.

It’s possible the JS http support will die followed by a statement that it has never been a supported feature in the sim.

Hi, yes you can pass structured data through SimConnect, for example a struct containing a mix of ints, doubles and strings. Not sure if it is possible from JS though but when communicating between a WASM module and an external EXE it is absolutely possible.

If you’re able to run a web server in your app, you can use either regular HTTP calls (fetch), or websockets from a JS instrument. It doesn’t need HTTPS, but does need correct CORs headers for some things.

For SimConnect and WASM instruments you can use client data areas to send structured data back and forth.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.