Search Unity

Posting JSON from unity to Google Appscript

Discussion in 'Scripting' started by tavarock, Apr 20, 2021.

  1. tavarock

    tavarock

    Joined:
    Oct 15, 2020
    Posts:
    1
    I'm trying to POST a JSON string to Google Appscript, but when parsing, it is not able to identify the needed features and looks like the whole string is being interpreted as the feature's name, as well as showing the JSON in different string format.

    I've tried different things and none of them has worked yet. Also, when printing directly the JSON.parse(jason.parameter) it is whon as follows:

    {"{\"Alumno\":\"6789\",\"Puntuacion\":\"-14\"}":""}

    The appscript code was previously tested by posting using python and works fine. In it, a variable is created and the parsed:

    var features = JSON.stringify(json.parameters)
    var myObj = JSON.parse(features)

    When printing myObj.Alumno and myObj they show as "undefined"

    If not stringified, the communication returns an error
    I also tried to use PUT instead of POST but I'm also getting an error "page not found"

    --------------------------------------------------------CS SCRIPT 1----------------------------------------------------

    Code (CSharp):
    1. public void SendInformation()
    2.     {
    3.         Name = name.text;
    4.         Score = score.text;
    5.      
    6.         MyClass myObject = new MyClass();
    7.      
    8.         myObject.Alumno = Name;
    9.         myObject.Puntuacion = Score;
    10.  
    11.         string json = JsonUtility.ToJson(myObject);
    12.         string url = "https://script.google.com/a/itesm.mx/macros/s/AKfycbxdsPdqFkV1ZO_LNbkrpRQyKO_kDPxpUy3sBeEjNw/exec";
    13.  
    14.         StartCoroutine(SendHttp(url, json));
    15.     }
    16.  
    17.     IEnumerator SendHttp(string url, string json)
    18.     {
    19.    
    20.         UnityWebRequest request = UnityWebRequest.Post(url, json);
    21.  
    22.         yield return request.SendWebRequest();
    23.  
    24.         Debug.Log("Status Code: " + request.responseCode);
    25.  
    26.         if (request.isNetworkError)
    27.         {
    28.             Debug.Log(request.error);
    29.         }
    30.         else
    31.         {
    32.             Debug.Log(request.downloadHandler.text);
    33.             Debug.Log(json);
    34.         }
    35.     }
    --------------------------------------------------------CS SCRIPT 2----------------------------------------------------
    Code (CSharp):
    1. public void SendInformation()
    2.     {
    3.         Name = name.text;
    4.         Score = score.text;
    5.      
    6.         MyClass myObject = new MyClass();
    7.      
    8.         myObject.Alumno = Name;
    9.         myObject.Puntuacion = Score;
    10.  
    11.         string json = JsonUtility.ToJson(myObject);
    12.         string url = "[URL]https://script.google.com/a/itesm.mx/macros/s/AKfycbxdsPdqFkV1ZO_LNbkrpRQyKO_kDPxpUy3sBeEjNw/exec[/URL]";
    13.  
    14.         StartCoroutine(SendHttp(url, json));
    15.     }
    16.  
    17.     IEnumerator SendHttp(string url, string json)
    18.     {
    19.    
    20.  
    21.         //Posting Manually
    22.      
    23.         var request = new UnityWebRequest(url, "POST");
    24.         byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
    25.         /equest.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
    26.         request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
    27.      
    28.         request.SetRequestHeader("Content-Type", "application/json");
    29.         yield return request.SendWebRequest();
    30.  
    31.         Debug.Log("Status Code: " + request.responseCode);
    32.  
    33.         if (request.isNetworkError)
    34.         {
    35.             Debug.Log(request.error);
    36.         }
    37.         else
    38.         {
    39.             Debug.Log(request.downloadHandler.text);
    40.             Debug.Log(json);
    41.         }
    42.     }}
     
    Last edited: Apr 20, 2021
  2. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Bunny83 likes this.