Search code examples
javaretrofitretrofit2

Is it possible in a Retrofit Post request to accept other data than the one sent?


In Java Android I use Retrofit. I need to post a request to send user data - first name, last name, age, phone number, password. And in response, his id and token should come.How to make such a request?


Solution

  • Make a class to represent the response.

    class RegisterResponse {
        private long id;
        private String token;
        public AuthResponse(long id, String token) {
            this.id = id;
            this.token = token;
        }
        public long getId() {
            return id;
        }
        public void setId(long id) {
            this.id = id;
        }
        public String getToken() {
            return token;
        }
        public void setToken(String token) {
            this.token = token;
        }
    }
    

    Then, your Retrofit method could look like this:

    @Post("/somepath")
    RegisterResponse register(@Query("firstname") String firstName, @Query("lastname") String lastName /* other parameters... */);
    

    You could also make another class to represent the body of the request rather than writing out each parameter separately.