Search Unity

UnityWebRequest.Post to send a task to ClickUp's API, encoding JSON improperly

Discussion in 'Scripting' started by ZoeLeet, May 24, 2022.

  1. ZoeLeet

    ZoeLeet

    Joined:
    Apr 1, 2021
    Posts:
    2
    So I'm writing a program in Unity that sends tasks to a ClickUp list. I've been able to send the request through Postman for testing properly, but whenever I try to send that request through Unity I get an error with the Json encoding:

    {"err":"Unexpected token % in JSON at position 0","ECODE":"JSON_001"}


    The code for the method is as follows. It's just a very basic tester method, so I know it's a little messy as is, but once I'm able to actually send the requests properly I want I'll re-write it with the full functionality.

    Code (CSharp):
    1. private void SendDemoTask()
    2.     {
    3.         string jsonString = "{\"name\": \"Unity send from postman\",\"description\": \"Sent on May 24 2022\"}";
    4.  
    5.         UnityWebRequest demoTaskRequest =
    6.             UnityWebRequest.Post($"https://api.clickup.com/api/v2/list/{listID}/task",jsonString);
    7.         demoTaskRequest.SetRequestHeader("Authorization",accessToken);
    8.         demoTaskRequest.SetRequestHeader("Content-Type","application/json");
    9.  
    10.         var operation = demoTaskRequest.SendWebRequest();
    11.  
    12.         // Wait for request to return
    13.         while (!operation.isDone)
    14.         {
    15.             CheckWebRequestStatus("Task creation failed.", demoTaskRequest);
    16.         }
    17.        
    18.         Debug.Log(demoTaskRequest.result);
    19.         Debug.Log(demoTaskRequest.downloadHandler.text);
    20.     }
    It seems to be an issue with the JSON encoding. Unfortunately the POST method doesn't have an argument to take a byte array. The PUT method does, but ClickUp's API won't accept the same types of requests through Put.

    Is there a way for me to send this request that will correct the encoding issue? Or is the problem somewhere else?

    Apologies if any part of this isn't clear. I'm fairly new to using UnityWebRequest and a total noob to webdev in general.

    Thank you for any help you all can offer, I very much appreciate it!
     
    ryanmillerca likes this.
  2. alexanderameye

    alexanderameye

    Joined:
    Nov 27, 2013
    Posts:
    1,383
    Not exactly an answer to the error you get, but I have been able before to send a byte array through a POST call like this:

    Code (CSharp):
    1. string requestURL = "https://a20artour.studev.groept.be/addTour.php";
    2.  
    3. Tour tour = new Tour
    4. {
    5.     name = tourName,
    6.     description = tourDescription,
    7. };
    8.  
    9. // convert object to JSON
    10. string jsonBody = JsonUtility.ToJson(tour);
    11.  
    12. // convert JSON to raw bytes
    13. byte[] rawBody = Encoding.UTF8.GetBytes(jsonBody);
    14.  
    15. UnityWebRequest request = new UnityWebRequest(requestURL, "POST");
    16. request.uploadHandler = new UploadHandlerRaw(rawBody);
    17. request.downloadHandler = new DownloadHandlerBuffer();
    18. request.SetRequestHeader("Authorization", "Basic " + GetAuthenticationKey());
    19. request.SetRequestHeader("Content-Type", "application/json");
    20.  
    21. yield return request.SendWebRequest();
    22.  
    23. if (request.isNetworkError || request.isHttpError)
    24. {
    25.   // error
    26.     // request.error
    27.     // request.responseCode
    28. }
    29.  
    30. else
    31. {
    32.     // successful
    33.     // request.downloadHandler.text
    34. }
    edit: Note that the 'Tour' class in this example is marked with [Serializable], this is required for the conversion to JSON. Not sure what is going wrong with your own code, but I guess making a class for the messages you send doesn't hurt? Could be as simple as a "MyMessage" class with just a "message" string field or something.
     
    MrD69, atfunity2, Bunny83 and 2 others like this.
  3. ZoeLeet

    ZoeLeet

    Joined:
    Apr 1, 2021
    Posts:
    2
    This worked, thank you so much!
     
    alexanderameye and ryanmillerca like this.
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,993
    This, or similar questions, has been asked countless of times now :)

    Yes, the static UnityWebRequest.Post method unintuitively expects URLEncoded form data, nothing else. So this method specifically applies url encoding to the string and also uses
    application/x-www-form-urlencoded
    as content type. This can be seen here. In most recent versions they actually marked "Post" as obsolete, so they may plan to remove it as the current behaviour is misleading.

    So yes, if you want to post raw text / string data you have to manually use a raw upload handler and set the content type yourself.