WebGL API Crash

Hello,

I am having an issue calling an API in WebGL. When I build the project as a WebGL all of the functions are fine. It is when I hit a button that calls the API to post data that crashes the project. Do I need to call the API through JS code to run this unity project on WebGL or is there any other idea of what could be causing this?

Thanks!

Hi! Can you share more details about this crash? what crashes, exactly? what errors do you see? can you share screenshots?

Hi!

I will provide screenshots later today since I cannot do so right now, but to give more details about the crash, basically I am entering information in a form into unity InputFields and that data is supposed to be posted/stored via the API. But when I click submit to submit the information, the entire project crashes on a WebGL application but not to any other applications (standalone, etc). In order for me to run the project again, I’d have to close out the build and rebuild the project. The only error I get is a “nullreferenceexception: object reference not set to an instance of an object” error and that is it.

Hi! Here is the Screenshot:

To visualize this better, data gets entered in each of the fields and this submit button sends this data and stores it via the API call. But when you hit this submit button when it is built in a WebGL environment, the entire WebGL application crashes and I would have to make a new tab to rerun the application. Let me know what you think.

Thanks!

7680454--960037--SWPicture.png

I’m not sure what you mean by the “API call” here. Is it an HTML POST request? Sharing a code snippet would be very helpful.

I am using C# to send a request to the Rest API to store all data entered in the InputFields. The request gets sent when I click the submit button.

I will be able to send a snippet of the code later today.

Attached below is a snippet of the code. The code in the screenshot is a picture of sending a request to the API (POST Method) which is in C# language.

7682593--960412--Snippet SWCode.png

HttpWebRequest is from System.NET, and you shouldn’t use that because JavaScript code does not have direct access to IP Sockets to implement network connectivity (see our docs page here: Unity - Manual: WebGL Networking)

You should use UnityWebRequest instead, and then see if the crash still occurs.

Does the same thing apply to HttpWebResponse?

Another thing to note is this project does not have any JavaScript added to the project, I was sending over this data as a JSON format as the requestBody. To use UnityWebRequest, do I need to use any sort of JavaScript for this? And also, is there any sample code that shows a demo of using UnityWebRequest? I checked the link to the documentation you sent but I didn’t see any sample code for it and was wondering where I can find some demo code to look at as well.

https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Post.html

I have tried modifying my code into the forum of sample Code from current documentation. Is there a way I can print out the response JSON from the request here? I’ve seen that you can use “Debug.Log(w.downloadHandler.text);” line to print it out but when doing this I am getting the following printed out in the console as shown in the screenshot. I’ve also provided the code that I used to execute the unity Project that gave me this response in the console. Could there be a reason to why this is happening? To me it looks like it is printing out an object but when I used ToString() at the end it still printed this out.

7691713--962473--ObjectMarketo.png
7691713--962476--SnippetCode postData.png

The error mentions line 35 of Assets/ColorSelection.cs, so take a look at that first - your snippet shot doesn’t include line numbers or what file it’s from.
Edit: also, I would look at what’s providing the response, because I use downloadHandler.text in numerous places without anything being returned but the actual response - not saying there isn’t an issue with it, just that I haven’t experienced any.

Also you could try this approach instead of what you’re doing, though I doubt it makes a difference, I work with responses like this:

// request = UnityWebRequest.Post(..
UnityWebRequestAsyncOperation op = request.SendWebRequest();
op.completed += (obj) =>
{
  Debug.Log(request.downloadHandler.text);
  request.Dispose();
};

Apologies about having that in the screenshot, the error for ColorSelection.cs is not the file that has this code in it. I should specify the one I was referring to was the one on the bottom of the console that says Debug.Log(object). I implemented the approach you sent like shown in the screenshot and it still printed it out the same output as the previous screenshot. I’m not sure if this has anything to do with it but I also provided the JSON format in this reply which is how I’m creating the requestBody that is being sent. The bodyBuilder method returns the JSON format and initializes requestBody String variable as the JSON.

7691872--962533--1 - SnippetCode postData.png
7691872--962536--2 - SnippetCode postData.png

I don’t think Dictionary can be serialized easily - unless I’m mistaken or that’s changed - a search turns up a number of solutions though.

Also, in your post data png, you’re calling SendWebRequest twice - you want to remove the yield line.

Edit: and you probably still want to be checking w.result like you did originally, I just omitted that for brevity.

I originally had only one SendWebRequest but the function itself required I have a value being returned which is why I added the yield line unless there is a different way I can return the value as such. And for the dictionary serialization, The function bodyBuilder serializes the Dictionary into a JSON correctly as far as I know since when I had my original code of using the HttpWebRequest method everything was working fine except on WebGL.

Okay - well if you’re keeping yield there anyway you could do it how you did originally. I mentioned it because that’s the only thing I do differently from the link I posted earlier and it has been working in WebGL for me. I can’t think of anything else to suggest.