Question by
Napoleonite · Jun 19, 2021 at 04:01 PM ·
webrequest
Send WebRequest with Dto/object in body
How do I send dto's (or any serializable class) to/from the server? The server doesn't use strings but uses dto's instead. I tried an upload handler and various tutorials and manuals but none work for objects, they all only work for strings?
The server is this:
[HttpPost]
public async Task<IActionResult> Login([FromHeader] string ip, UserCredentialsMinimalDto? userCredentials)
{
..
}
Unity code:
private IEnumerator SendTokenRequest(string uri)
{
using (UnityWebRequest webRequest = UnityWebRequest.Post(uri, "POST"))
{
webRequest.SetRequestHeader("content-Type", "application/json");
webRequest.SetRequestHeader("Accept", "application/json");
webRequest.SetRequestHeader("ip", "localhost"); // TODO: For testing.
byte[] bytesToSend = new System.Text.UTF8Encoding().GetBytes(JsonUtility.ToJson(new UserCredentialsMinimalDto("a", "1")));
webRequest.uploadHandler = (UploadHandler) new UploadHandlerRaw(bytesToSend);
yield return webRequest.SendWebRequest();
string[] pages = uri.Split('/');
int page = pages.Length - 1;
switch (webRequest.result)
{
case UnityWebRequest.Result.ConnectionError:
case UnityWebRequest.Result.DataProcessingError:
UnityEngine.Debug.LogError(pages[page] + ": Error: " + webRequest.error);
break;
case UnityWebRequest.Result.ProtocolError:
UnityEngine.Debug.LogError(pages[page] + ": HTTP Error: " + webRequest.error);
break;
case UnityWebRequest.Result.Success:
UnityEngine.Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
break;
}
}
Comment
Your answer

Follow this Question
Related Questions
UnityWebRequest Responds with 405 1 Answer
UnityWebRequist Return 403 0 Answers
How do you disable Unity Analytics in Unity Pro? 1 Answer
Android HTTPS POST Request 0 Answers