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 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;
}
}
↧