private IEnumerator beginSave(Weapon weapon, LoginAuth loginAuth) { WeaponDTO dto = weapon.serialize(); string weaponJson = JsonUtility.ToJson(weapon); Debug.Log(weaponJson); byte[] weaponJsonByte = System.Text.Encoding.UTF8.GetBytes(weaponJson); //byte[] testData = System.Text.Encoding.UTF8.GetBytes("helloUnity"); UnityWebRequest www = UnityWebRequest.Put(saveWeaponUri + weapon.id, weaponJsonByte); www.SetRequestHeader("Authorization", "Token token=" + loginAuth.auth_token); // this is key for rails to know what kind of data to even think how to start parsing www.SetRequestHeader("Content-Type", "application/json"); www.downloadHandler = new DownloadHandlerBuffer(); yield return(www.SendWebRequest()); if (www.isNetworkError) { Debug.Log(www.error); } else { Debug.Log("weapon update success"); // Debug.Log (www.downloadHandler.text); NetworkResponses rd = JsonUtility.FromJson <NetworkResponses>(www.downloadHandler.text); Debug.Log(rd.success [0]); } }
/// <summary> /// create new user using Post /// </summary> /// <returns>The new user creation.</returns> /// <param name="loginAuth">Login auth.</param> private IEnumerator beginNewUserCreation(LoginAuth loginAuth) { // WWWForm worked with creating new user, on rails side the escaped charc will be removed so that rails will work // if rails is expecting the data to be one layed nested, fix an if else on rails registeration side // by not requiring the "user" // first key param i.e. { "user": {"email": "*****@*****.**", "password": "******", "password_confirmation": "password" } } // fill the form - with Post on rail side, using the form, the escaped characters are removed which is good WWWForm form = new WWWForm(); form.AddField("email", loginAuth.email); form.AddField("password", loginAuth.pass); form.AddField("password_confirmation", loginAuth.pass); UnityWebRequest www = UnityWebRequest.Post(createUserUri, form); www.downloadHandler = new DownloadHandlerBuffer(); // no need for to set header content type // as these will only cause rails to fail to parse the string when using post calls // or use the default content type by not setting it at all // www.SetRequestHeader("Content-Type", "application/json"); //www.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded"); yield return(www.SendWebRequest()); // response - check the callback request if (www.isNetworkError) { Debug.Log(www.error); } else { string returnedJsonData = www.downloadHandler.text; // to handle the json deserialize into an object // TODO not sure how we want to handle return data yet from rails /// whether to use jsonUtility or a true jsonObject NetworkResponses rd = JsonUtility.FromJson <NetworkResponses>(returnedJsonData); // convert the response from json into real object temp auth //LoginAuth tempAuth = JsonConvert.DeserializeObject<LoginAuth>(returnedJsonData); // store the login auth for later // this.loginAuth = loginAuth; // this.loginAuth.auth_token = tempAuth.auth_token; Debug.Log("creation success"); Debug.Log(returnedJsonData); //Debug.Log (rd.email[0]); } }