- Home /
UnirtyWebRequest.Post sending empty uploadHanlder.data
Hi, A have a problem with unityWebRequest.Post, in the capture it says "Put" but I changed to test a solution in other foums but is not working, The problem is that the data is no being populated The json file prints fine but the uploadHandle.data is not, in the second error in the console you can see is the same as before the HTTP/1.1 500 Internal Server Error. I have tried WWWForm, the same HTTP 500 error.
The backend developer says with Postman is correct, and I have checked the json names and order.
Some links to solutions that not work for me :C
https://stackoverflow.com/questions/60862424/how-to-post-my-data-using-unitywebrequest-post-api-call
https://forum.unity.com/threads/unitywebrequest-post-returning-empty-string.632299/
I appreciate any help
Answer by Bunny83 · Jun 02, 2021 at 09:51 PM
Well, first of all, please do not post code or error messages as images. They are hard to read and can not be quoted.
Next, you can not Debug.Log a byte array- It won't print out any of the content, just the type name so this is pretty useless for debugging purposes. You may print out the length of the array and maybe convert the byte array into something readable like a hex string or just print out the individual byte values.
In order to debug your issue you may want to use a packet analyser like wireshark to see how the actual requests look like. Alternatively, if your backend supports CORS you could build your application for WebGL and test it inside FireFox or Chrome. There you can use the build-in developer tools to inspect the requests.
Finally I'd like to add that the backend doesn't seem to be very stable when it throws an internal server error on a possibly malformed request. Internal server error could be caused by anything on the server side.
ps: The code in your screenshot that the creation of the upload handler commented out. So of course the code you've posted won't work as you never upload the data.
pps: you can use this method to "log" your byte array in order to "inspect" it:
public static string FormatByteArray(byte[] aData)
{
if (aData == null)
return "byte array is null";
var sb = new System.Text.StringBuilder();
sb.Append("byte array ( length: ").Append(aData.Length).Append(") ").AppendLine();
int count = 0;
foreach (var b in aData)
{
sb.AppendFormat("{0,2:X2} ", b);
if (++count % 16 == 0)
sb.AppendLine();
}
return sb.ToString();
}
So just insead of doing
Debug.Log("bytes: " + yourByteArray);
you would do
Debug.Log("bytes: " + FormatByteArray(yourByteArray));
If you need further assistence you should post the actual log text (make sure you format it as code here on UA with the 101010 button after you select the text). Also if the backend is developed by someone else, it would help if the developer could give you a concrete example of a body that does work.
Thanks for you detailed response!
I noted the debug.log bytes[] after I posted this, now I can see the what is sending in the post, thanks anyways.
Sorry for the images...
I talked with the backend developer and copy-paste the generated json and she tried in postman and the post done correctly, I tried with a empty json in unity and the post is done correctly... but how the json in unity not work but in postman?
string jsonOrder = JsonUtility.ToJson(order, true);
//string jsonOrder = JsonUtility.ToJson("Test");
UnityWebRequest request = UnityWebRequest.Post(url, "POST");
//request.chunkedTransfer = false;
//request.useHttpContinue = false;
request.SetRequestHeader("Content-Type", "application/json");
byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonOrder);
request.uploadHandler = new UploadHandlerRaw(bodyRaw);
Debug.Log("Data after: \n" + System.Text.Encoding.UTF8.GetString(request.uploadHandler.data, 0, request.uploadHandler.data.Length));
request.downloadHandler = new DownloadHandlerBuffer();
//request.SetRequestHeader("Content-Type", "application/json");
//request.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(jsonOrder)) as UploadHandler;
Debug.Log("Json: \n" + jsonOrder);
//Debug.Log("BodyRAw: " + bodyRaw);
yield return request.SendWebRequest();
if (request.isNetworkError || request.isHttpError)
{
ordering = false;
Debug.LogError(request.error);
Debug.LogError(System.Text.Encoding.UTF8.GetString(request.uploadHandler.data, 0, request.uploadHandler.data.Length));
}
else
{
ordering = false;
Debug.Log("<color=yellow>Order posted correct!</color>");
Debug.LogError(System.Text.Encoding.UTF8.GetString(request.uploadHandler.data, 0, request.uploadHandler.data.Length));
}
I wil try with wireshark, thanks! maybe you can point me out how to do this...? just install and check the network traffic?