4

I am using Retrofit like this to get all the books and delete all the books.

 @GET("/books")
    BookListResponse getAllBooks();
    @DELETE("/clean")
    Response deleteAllBooks();

But an error status 500 (internal server error) was returned. I tested these two restful calls using Chrome restful client app and they work properly.

However, if I just want to get one book or delete one book like this

 @GET("books/1")
    BookResponse getOneBook();
    @DELETE("books/1")
    Response deleteOneBook();

They can work properly.

So I am not sure if that's the server issue or I have missed something?

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
klabe
  • 455
  • 1
  • 5
  • 13
  • Even if you have missed something, Server side code should not break. Clearly it's a server side bug. – Arun Aug 24 '15 at 13:07

3 Answers3

5

This is just a matter of semantics: when you say to your server "Please delete book", but you don't say which book you want to delete, the server don't know what to do (this happens when you send a DELETE to /books). Hence the error 500. But when you say "Please delete book with id 1", now the server knows what to do (this happens when you send a DELETE to /books/1).

It is ok to work that way if you setup your server like this, but I've never seen any REST service to delete ALL records of a particular model. Again, if you coded your server this way it is ok, just make sure the request is reaching the server the way you want.

After all, if you got a error 500, check your server. The problem isn't in the Android side, definitely.

Elvis Fernandes
  • 1,108
  • 10
  • 21
  • 1
    Hi, I solved it by adding an accept header to the request and set the header value to application/json. The server APIs were not written by me, so I didn't have any control of them. – klabe Apr 12 '14 at 22:14
  • Vote up for nice explanation. Even if you have missed something, Server side code should never break. There was a bug in the server side code. api Guy need to fix it. – Arun Aug 24 '15 at 13:15
  • Vote up for nice explanation – sasikumar Nov 07 '16 at 06:12
4

Just add the header accept in the request as follow:

Request request = new Request.Builder()
                .url(urlString)
                .header("accept", "application/json")
                .build();
isaacfi
  • 190
  • 1
  • 9
4

Update
In Retrofit 2.1 the request builder has changed so you have to specify header in the retrofit interface methods

@Headers("Accept: application/json") @GET("products") Call<Products> getAllProducts();

Gowsik
  • 1,096
  • 1
  • 12
  • 25