0

The Unity C# code below is sending a username and a new and old password to a server that should be using PUT to update the database. The body in the request is blank on the server. uriBase is the base domain for the endpoint. The only way I can get my data to the server is to use URL parameters like a GET and using reqest.query to get the values but that is not the proper way to do it. Server is running NodeJs and MongoDB. Any ideas why the body would be received as empty? Alternatively, if anyone has any better suggestions without buying a Unity Asset to get the put request to work correctly, that would be appreciated. Thanks guys.

IEnumerator changePasswordCallout() {
    string jsonRequest = "{\"username\":\"" + UsernameField.text + "\",\"oldPassword\":\"" + OldPasswordField.text + "\",\"newPassword\":\"" + NewPasswordField.text + "\"}";
    byte[] body = System.Text.Encoding.UTF8.GetBytes(jsonRequest);

    UnityWebRequest request = UnityWebRequest.Put(uriBase + "/password/change", body);
    yield return request.SendWebRequest();
    if (request.isNetworkError || request.isHttpError) {
        // error code
    } else {
        // not error code
    }
}
user3648673
  • 45
  • 1
  • 8
  • The API expects a json body? – Chetan May 02 '18 at 19:33
  • The string being used as the body is a json string. Unity only supports string or byte[] as body parameters, which is why im confused about this. Neither string or byte[] works. My assumption (going along with what you say) is that the unity put method doesn't convert it to a json body like it should. – user3648673 May 02 '18 at 19:41
  • https://www.google.com.sg/amp/s/codetolight.wordpress.com/2017/01/18/unity-rest-api-interaction-with-unitywebrequest-and-besthttp/amp/ – Chetan May 02 '18 at 19:47
  • Thanks for sharing that link. I hadn't run across that while researching. Unfortunately, that is using a unity asset which I don't want to pay for. – user3648673 May 02 '18 at 20:22
  • You need to set the *"Content-Type"* header as *"application/json"* See the *POST request with Json:* section from the duplicate. You can easily change that to PUT request. – Programmer May 02 '18 at 23:29

0 Answers0