0

I am trying to send data with unitywebrequest and I tried both sending it with raw json or with a WWWForm, both cases create an entry in the database but the values are all null. I check the json and form before hand and they both have initialized values. So I have no idea why the entry is null.

    public static IEnumerator SendUserData(string json)
    {
        WWWForm form = new WWWForm();
        var headers = form.headers;
        headers["X-Parse-Application-Id"] = "AppId";
        headers["X-Parse-REST-API-Key"] = "RestKey";
        headers["Content-Type"] = "application/json";
        User_Data data = JsonUtility.FromJson<User_Data>(json);
        form.AddField("id", data.id);
        form.AddField("age", data.age);
        form.AddField("location", data.location);
        form.AddField("gender", data.gender);
        form.AddField("nationality", data.nationality);
        form.AddField("language", data.language);
        Debug.Log(json);

        using (UnityWebRequest req = UnityWebRequest.Post(url + "/user/create", form))
        {
            req.SetRequestHeader("Access-Control-Allow-Credentials", "true");
            req.SetRequestHeader("Access-Control-Allow-Origin", "*");
            req.SetRequestHeader("Access-Control-Allow-Headers", "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time,Authorization");
            req.SetRequestHeader("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
            req.SetRequestHeader("content-type", "text/json");
            yield return req.SendWebRequest();
            if (req.isHttpError || req.isNetworkError)
                Debug.LogError("Error: " + req.error);
            else
                Debug.Log("Form send succesfully.");

        }
}

I have tried doing it with and without the headers. Same issue. I tried changing the content-type when I use form to multipart/form-data and application/json for json but still the same issue. Any help is appreciated. Thank you.

Note 1: I receive no errors by the way on unity.

Note 2:The number autoincrements that's why its not null.

Note 3: Postman works correctly with the same json that is created in unity. Image of the database

  • I've had a similar issue recently, this post gave me answers https://stackoverflow.com/questions/9145667/how-to-post-json-to-a-server-using-c – zambari Jun 03 '22 at 11:47
  • If you have the string as json why reinvent the wheel. You turn it to data and the insert it in a form which was looking for a json string. – BugFinder Jun 03 '22 at 15:01

0 Answers0