WebGL UnityWebRequest.Post in Firefox returning null from web service
Hi all
I have a web API service with a POST endpoint:
http://mobileappsapi.azurewebsites.net/api/Leaderboards/GetTopThreeLeaderboardItem/
It takes a x-www-form-urlencoded body element "ClientID" with a value of 0.
This works fine in Postman.
I have a game here: http://mobileappsapi.azurewebsites.net/unity/santasaveschristmas/
There is a leaderboard at the bottom left of the start screen which is fed by a UnityWebRequest.Post request to the endpoint.
This works in Chrome, but in Firefox the response showing from the POST to the endpoint in Firebug is null.
If I "resend" the request in Firebug it returns valid request data as JSON as it should do. So I guess there is something in the way that Unity is actually making the call.
Here is the C# method that calls the DoHttpPostRequest method to return the C# LeaderboardItemList object:
public IEnumerator GetTopThreeLeaderboardItems(System.Action<LeaderboardItemList> result)
{
string DataUrl = string.Format("{0}Leaderboards/GetTopThreeLeaderboardItem/", GameStartController.APIUrl);
string resultString = "";
WWWForm PostData = new WWWForm();
PostData.AddField("ClientID", GameStartController.ClientID);
yield return StartCoroutine(DoHttpPostRequest(DataUrl, PostData, value => resultString = value));
LeaderboardItemList rs = JsonUtility.FromJson<LeaderboardItemList>(resultString);
result(rs);
}
Here is the C# DoHttpPostRequest method in Unity:
public IEnumerator DoHttpPostRequest(string url, WWWForm PostData, System.Action<string> result)
{
UnityWebRequest www = UnityWebRequest.Post(url, PostData);
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else {
//Debug.Log("HTTP POST request done SUCCESS.");
result(www.downloadHandler.text); // Pass retrieved result.
}
}
I don't think it's related, but for some reason the .mem file request is returning a 404 in firebug. The mime type in the web.config file is
<staticContent>
<mimeMap fileExtension=".data" mimeType="application/octet-stream" />
<mimeMap fileExtension=".mem" mimeType="application/octet-stream" />
<mimeMap fileExtension=".memgz" mimeType="application/octet-stream" />
<mimeMap fileExtension=".datagz" mimeType="application/octet-stream" />
<mimeMap fileExtension=".jsgz" mimeType="application/octet-stream" />
</staticContent>
Any ideas please?
Thank you all very much :)
UPDATE (SOLVED!): It ended up being a really simple issue (as is almost always the case). The Web API written with ASP.NET Web API 2 was returning xml rather than json when the request was from Firefox for some reason.
The solution was just to force the api to only return json. This was achieved by adding the following to the WebApiConfig.cs file
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("text/html"));
It now works perfectly :) I hope this helps someone else out there.
Your answer
