Unity WebGL does not Post to Azure Function app: form already read error

有些话、适合烂在心里 提交于 2020-04-12 07:24:26

问题


Tested for Unity 2019.3.0f3 or 2019.3.0f5 or 2019.3.9 and Azure Functions V3.

I Post from a Unity WebGL project to an azure function as form.

I can't get the post into variables in the Function app: the Function app throws http 500 with "Unexpected end of Stream, the content may have already been read by another component.". So when webgl project is run on browser, chrome shows 500 internal error; firefox doesn't show the data on console but neither shows the error but you can see http 500 error with Fiddler.

Project runs on Editor and Postman.

Function app is now running and publicly available to test at https://unitywebglbugtest.azurewebsites.net/api/TestBug

public class Function1
    {
        [FunctionName("TestBug")]
        public async Task<HttpResponseMessage> 

    TestBug([HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req, ILogger log)
            {
                string myUsername = Convert.ToString(req.Form["clientusername"]);
                return new HttpResponseMessage(HttpStatusCode.OK)
                {
                    Content = new StringContent($"Result: {myUsername}", Encoding.UTF8, "text/plain")
                };
            }
    }

Unity Webgl built and hosted project you can run to test: https://webglappbugtest.azurewebsites.net

Sample Unity submit form:

List<IMultipartFormSection> formData = new List<IMultipartFormSection>();
    string stringTest = "hello@gmail.com";
    formData.Add(new MultipartFormDataSection("clientusername", stringTest, "text/plain"));
    UnityWebRequest www = UnityWebRequest.Post("https://unitywebglbugtest.azurewebsites.net/api/TestBug", formData);
    Debug.Log("Now sending form " + stringTest);
    yield return www.SendWebRequest();
    string queryResult = www.downloadHandler.text;
    Debug.Log(queryResult);

Postman result:

EDIT: Looks like post sent is not being converted in some systems when sent out. Happens to a few systems only, hence this works for some but not all. Here's what Fiddler is sending. The issue has recently been flagged as a bug in this Unity post. Unfortunately the suggested patch didn't work for me on Unity 2019.3.0f3,2019.3.0f5, 2019.3.9

Unfortunately the query param solution is unsafe and relevant to avoid as seen in the recent Zoom hack

I've raised this bug with Unity.

来源:https://stackoverflow.com/questions/61113793/unity-webgl-does-not-post-to-azure-function-app-form-already-read-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!