I want to ask how to work correctly with the PUT method in Retrofit2 , I wrote the code, but I am not sure that it is correct and I would like to know how to correctly rewrite this code using RxJava .

 @PUT("{username}") Call<Void> updateUser(@Path("username") String username,@Body User user); 

And application:

  private void updateUserInfo() { User user = new User("iryna", "Alala", "alala@gmail.com", "some properties" ); Call<Void> deleteRequest = App.service.updateUser("bala", user); deleteRequest.enqueue(new Callback<Void>() { @Override public void onResponse(Call<Void> call, Response<Void> response) { // use response.code, response.headers, etc. } @Override public void onFailure(Call<Void> call, Throwable t) { // handle failure } }); } 

    1 answer 1

    do not forget about authorization

     @PUT("{username}") Observable<Response<Void>> updateUser(@Path("username") String username, @Body User user, @Header("Authorization") String auth); 

    and

     private void updateUserInfo() { User user = new User("iryna", "Alala", "alala@gmail.com", "some properties" ); yourService.updateUser("bala", user ...) .(...) .subscribe(response -> { // use response.code, response.headers, etc. }, throwable -> { // handle failure }); } 
    • if a specific response from the server comes, then replace Response <Void> with your response - Maxim Kuznetsov
    • Why use PATCH and not PUT? What will the implementation of the method look like when it is Observable? - Lucky_girl
    • one
      PATCH update only changes the field. B User should only change the field, leave the rest null. stackoverflow.com/questions/24241893/rest-api-patch-or-put - Maxim Kuznetsov
    • So, if the API says that there should be a PUT method, and I will use PATCH - will this not cause an error? - Lucky_girl
    • one
      if in api PUT, then use PUT. - Maxim Kuznetsov