I am trying to POST a json string to a web API and currently got this code:
async Task<Uri> Post(CapturedImage image)
{
string json = JsonConvert.SerializeObject(image);
var content = new StringContent(json.ToString(), Encoding.UTF8, "application/json");
HttpResponseMessage response = await client.PostAsync(url, content);
response.EnsureSuccessStatusCode();
Debug.Log("Request Message Information:- \n\n" + response.RequestMessage + "\n");
Debug.Log(json.ToString());
// return URI of the created resource.
return response.Headers.Location;
}
The code is not done yet so I'm not sure this is the type I want to return in the end (in the end I will have a new json string with information about a specific car supposedly).
Unity hangs when it comes to the line HttpResponseMessage response = await client.PostAsync(url, content); and I have to forcefully close the unity app.
How can I use the httpClient successfully? I am not using the unityWebRequest for the moment because I don't understand in which way WWWForm is being sent (like an object I guess). And I don't want a byte[] to be sent either, but a json string and I can assume that the WWWForm is like a json string but separated-ish. Also I don't get which type to be received in the web API either when its a WWWForm. Like if its a json string I just have the parameter like (string json).
Am I thinking completely wrong with not using unityWebRequest instead of httpClient? I have to use json if possible.
UnityWebRequest.Post(string URL, string data)also takes a simplestringas input data not only aWWWForm.I don't want a byte[] to be sent either- well in the end everything you send will be encoded tobyte[]...StartCoroutine()and so instead of await async? What can I exchangeUnityWebRequestwith in the code? Can you show some code example? @BugFinder