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/51526506/sending-unity-web-request-post-returning-empty-from-webhost

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/


181577-webrequestproblemdebug.png

I appreciate any help

1 Answer

1

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.