Search Unity

UnityWebRequest.Post(url, jsonData) sending broken Json

Discussion in 'Multiplayer' started by Olly, Jul 2, 2016.

  1. gangafinti

    gangafinti

    Joined:
    Jun 13, 2013
    Posts:
    19
  2. PHL1

    PHL1

    Joined:
    Jul 25, 2017
    Posts:
    29
    ok code runs perfectly in Windows but when i use it in android apk i get error Bad Request again.put is not working as well. any idea to fix this bug?
    version: 2019.1.9f1
    android version 7.0
     
    sandolkakos likes this.
  3. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    It has already sometime I don't use the POST method in my projects, but I know that Unity changed a little bit the UnityWebRequest and added a new method only for Post. Here you are the Documentation about it:
    https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Post.html
     
    PHL1 likes this.
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,732
    Can you try the same request using latest Unity version? It's likely this issue has been fixed in 2019.2.
     
    PHL1 likes this.
  5. Saulius

    Saulius

    Joined:
    Aug 10, 2010
    Posts:
    21
    Happy it was useful.
     
  6. youtpout

    youtpout

    Joined:
    Jun 14, 2015
    Posts:
    2
    Hello,
    I don’t use UnityWebRequest for call rest, I use the classic c# implemetation (HttpClient) who works fine, you need to choose Net Standard for use it.
     
    PHL1 likes this.
  7. PHL1

    PHL1

    Joined:
    Jul 25, 2017
    Posts:
    29
    hi.sorry for late response.
    i installed unity 2019.2.10f1 and its working.thank you
     
    sandolkakos likes this.
  8. dvalles

    dvalles

    Joined:
    Nov 15, 2014
    Posts:
    11
    For those of you looking to offload the boilerplate of a C# solution. You can just force Unity to send utf-8 escaped data by setting the content-type to application/nothing (which is nonsense), then on the javascript backend decode it to JSON. Code below.

    Client: Minimal boilerplate, could even wrap
    UnityWebRequest www = UnityWebRequest.Post(path, jsonString);
    www.uploadHandler.contentType = "application/nothing";

    Backend: In some express middlware
    var decode_utf8 = function decode_utf8(s) {
    return decodeURIComponent(unescape(s));
    };

    req.body = decode_utf8(req.body);
    req.body = JSON.parse(req.body);
    return next();
     
  9. rvejendla

    rvejendla

    Joined:
    Oct 8, 2020
    Posts:
    1
    Working Perfectly to mine alos man. thank you
     
    sandolkakos likes this.
  10. sandolkakos

    sandolkakos

    Joined:
    Jun 3, 2009
    Posts:
    285
    Nice to know it worked.
    It has already some time I don't use the POST method in my projects, but I know that Unity changed a little bit the UnityWebRequest and added a new method only for Post. Here you are the Documentation about it:
    https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Post.html
     
  11. Champilas

    Champilas

    Joined:
    Aug 23, 2020
    Posts:
    2
    You saved me the life again. 5 years later haha Thank you. (this is the second time you saved me)
     
    v-strelchenko and sandolkakos like this.
  12. v-strelchenko

    v-strelchenko

    Joined:
    Aug 2, 2019
    Posts:
    13

    Hello from the year 2021 ;) The UnityWebRequest.Post with JSON data still doesn't work. This answer with small alterations does. Thank you!


    Code (CSharp):
    1. using var postRequest = new UnityWebRequest();
    2.         postRequest.url = PostUri; // PostUri is a string containing the url
    3.         postRequest.method = "POST";
    4.         postRequest.uploadHandler = new UploadHandlerRaw(Encoding.UTF8.GetBytes(postData)); // postData is Json file as a string
    5.         postRequest.downloadHandler = new DownloadHandlerBuffer();
    6.         postRequest.SetRequestHeader("Content-Type", "application/json");
    7.         postRequest.SendWebRequest();
     
  13. stickylab

    stickylab

    Joined:
    Mar 16, 2016
    Posts:
    92
    need help ,,how to reset joystick to zero ,,, dissalowing the hold