Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by _DS_1 · Jun 02, 2021 at 09:06 PM · networkingjsonwebrequest

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/

alt text alt text

I appreciate any help

webrequestproblem.png (59.0 kB)
webrequestproblemdebug.png (46.1 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image _DS_1 · Jun 02, 2021 at 10:45 PM 0
Share

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?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

167 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can you post a json string as a body in Unity? 2 Answers

how to find if WWW Post Request is succesful? 2 Answers

Performing simple network call to retreive JSON? 1 Answer

OAuth 2.0 Capture Redirect 0 Answers

New UnityWebRequest How Can I Handle the responses ?? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges