To keep things short trying to get a Unity client to work. It is making a simple Get request to: "http://localhost:8000/api/board". This request works fine:
- in my browser
- postman
- .net's HttpClient inside Unity editor mode
- if I build and run a WebGL build the resulting web page will work (had to add CORS settings to server first)
It DOES NOT work for UnityWebRequest inside Unity Editor mode though which is very frustrating, building webgl takes forever.
I have no idea why it won't work. Other routes like "https://httpbin.org/get" work fine in editor mode. I feel like it has something to do with UnityWebRequest. I've tried turning off firewall as well. I'm using Unity 2021.2.7f1, this was all ran inside the editor while building for windows.
Below works fine:
HttpClient client = new HttpClient();
System.Uri uri = new System.Uri("http://localhost:8000/api/board");
string responseBody = await client.GetStringAsync(uri);
Below fails with "Cannot connect to destination host" error response:
System.Uri uri = new System.Uri("http://localhost:8000/api/board");
UnityWebRequest www = UnityWebRequest.Get(uri);
UnityWebRequestAsyncOperation operation = www.SendWebRequest();
while (!operation.isDone)
await Task.Yield();
if (www.result != UnityWebRequest.Result.Success)
{
// Error stuff
}
EDIT:
FIX
It turns out the issue was the server which I made in Rust Rocket. I had it set to the default address = "localhost"
. I changed it to address = "0.0.0.0"
and now it works fine in editor play mode. I have no idea why 0.0.0.0 worked but localhost didn't.