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----------------------------------------------------
public void SendInformation()
{
Name = name.text;
Score = score.text;
MyClass myObject = new MyClass();
myObject.Alumno = Name;
myObject.Puntuacion = Score;
string json = JsonUtility.ToJson(myObject);
string url = "https://script.google.com/a/itesm.mx/macros/s/AKfycbxdsPdqFkV1ZO_LNbkrpRQyKO_kDPxpUy3sBeEjNw/exec";
StartCoroutine(SendHttp(url, json));
}
IEnumerator SendHttp(string url, string json)
{
UnityWebRequest request = UnityWebRequest.Post(url, json);
yield return request.SendWebRequest();
Debug.Log("Status Code: " + request.responseCode);
if (request.isNetworkError)
{
Debug.Log(request.error);
}
else
{
Debug.Log(request.downloadHandler.text);
Debug.Log(json);
}
}
--------------------------------------------------------CS SCRIPT 2----------------------------------------------------
public void SendInformation()
{
Name = name.text;
Score = score.text;
MyClass myObject = new MyClass();
myObject.Alumno = Name;
myObject.Puntuacion = Score;
string json = JsonUtility.ToJson(myObject);
string url = "[URL]https://script.google.com/a/itesm.mx/macros/s/AKfycbxdsPdqFkV1ZO_LNbkrpRQyKO_kDPxpUy3sBeEjNw/exec[/URL]";
StartCoroutine(SendHttp(url, json));
}
IEnumerator SendHttp(string url, string json)
{
//Posting Manually
var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = Encoding.UTF8.GetBytes(json);
/equest.uploadHandler = (UploadHandler) new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler) new DownloadHandlerBuffer();
request.SetRequestHeader("Content-Type", "application/json");
yield return request.SendWebRequest();
Debug.Log("Status Code: " + request.responseCode);
if (request.isNetworkError)
{
Debug.Log(request.error);
}
else
{
Debug.Log(request.downloadHandler.text);
Debug.Log(json);
}
}}