Retrofit POST Method with Json data got error code 400 : Bad Request

前端 未结 3 1788
暗喜
暗喜 2021-01-01 05:55

I would like to call a POST Method(Magento REST API) in Retrofit with a JSON data(provide JSON as JsonObject). For that, I call as follow from the postman and work fine for

3条回答
  •  自闭症患者
    2021-01-01 06:29

    Change your code to something like this, GsonConverterFactory converts a User object to json itself.

    public interface APIService {
        @POST("seq/restapi/checkpassword")
        @Headers({
            "Content-Type: application/json;charset=utf-8",
            "Accept: application/json;charset=utf-8",
            "Cache-Control: max-age=640000"
        })
        Call savePost(@Body User user);
    }
    

    This is User Class:

    public class User {
        private String username;
        private String password;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

提交回复
热议问题