UnityWebRequest.Post not passing parameters to Web API
I have created a simple WebAPI that I have working with the following postman call
[POST] http://localhost:58452/api/getGameData?TransactionId=123456789&LevelName=Test&DeviceType=Android
Works great... no issues in postman what so ever.
From within Unity... WWWForm form = new WWWForm(); form.AddField("TransactionId", "123456789"); form.AddField("LevelName", "Test"); form.AddField("DeviceType", "Android");
UnityWebRequest www = UnityWebRequest.Post("http://localhost:58452/api/getGameData", form);
yield return www.SendWebRequest();
I'm able to debug into the web api code on the server and each time the passed in variables are null. This works fine in postman so I'm not sure what I'm missing here on the Unity side. Any help would be appreciated.
Answer by Robert_Hildebrand · Jul 12, 2020 at 02:07 PM
I solved this issue.... the problem was on my end of course. Unfortunately I based my method on a snippet of code that I found on the web that was inaccurate.
I needed to specify that the parameters on the server side should be mapped from the form object and not the query string as follows: [HttpPost] public string Post([FromForm] string TransactionId, [FromForm] string PackageName, [FromForm] string DeviceType)
I then changed my postman code to use form data variables in the body and everything worked as it should.
Your answer
Follow this Question
Related Questions
UnityWebRequest Responds with 405 1 Answer
UnityWebRequist Return 403 0 Answers