简体   繁体   中英

Retrofit post request: 500 internal server error

Every time I try to send a post request, I get 500 error. Get requests work fine. Post request via postman works fine too, so there is no problems on the serverside. What is the problem?

Request code:

    HseDayApi hseDayApi = HseDayApi.retrofit.create(HseDayApi.class);
    ApiPostComment comment = new ApiPostComment();
    comment.setAuthor("Author");
    comment.setContent("Test");
    comment.setEventid(123);
    Call<ResponseBody> postComment = hseDayApi.postComment(comment);
    postComment.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Log.d("myLogs", response.toString());
            Log.d("myLogs", String.valueOf(response.errorBody()));
            Log.d("myLogs", String.valueOf(response.code()));
            Log.d("myLogs", response.toString());
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {

        }
    });

Logs say this

D/myLogs: retrofit2.Response@a1089f6
D/myLogs: okhttp3.ResponseBody$1@d57e1f7
D/myLogs: 500
D/myLogs: retrofit2.Response@a1089f6

Post request declaration:

@POST("/api/comments/add/text")
Call<ResponseBody> postComment(@Body ApiPostComment comment);

Class for request code:

public class ApiPostComment {
private int eventid;
private String author;
private String content;

public void setEventid(int eventid) {
    this.eventid = eventid;
}

public void setContent(String content) {
    this.content = content;
}

public int getEventid() {
    return eventid;
}

public String getAuthor() {
    return author;
}

public String getContent() {
    return content;
}

public void setAuthor(String author) {
    this.author = author;
}

}

Request result via Postman

remove / . as retrofit Baseurl ends with /. so ur url is now like baseurl//api/comments/add/text

@POST("api/comments/add/text")<-------- change
Call<ResponseBody> postComment(@Body ApiPostComment comment); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM