Stefano Saitta
Stefano Saitta

Reputation: 2014

What i have to return after a DELETE?

I'm wondering what I have to return after that I call my REST API using the DELETE method. I wasn't able to find out any standard/best practice for this. At the moment my code base use 2 different approach, first of all return the deleted resource so into the Response Body I return just null. The second approach (which I don't really like) I instance a new Object and I return it. What you do think is the best way? If none of this two seems good to you, which one would be the best (practice) approach?

Here a sample of what I actually have: code sample

NB: Of course both of the described approach, are performed after the actual deleting on the DB.

Upvotes: 1

Views: 4944

Answers (3)

Opal
Opal

Reputation: 84766

After successful deletion you should return empty body and 204 No Content status code.

When returning 200 OK with empty body some clients (e.g. EmberJS) fail because they expect some content to be parsed.

Upvotes: 6

Rafik BELDI
Rafik BELDI

Reputation: 4158

what about Returning void wich means HTTP 200 OK in case of success

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable("id") Long id) {
        service.delete(id);
    }

EDIT In the front Controller you can use something like this:

@RequestMapping(...)
public ModelAndView deleteMySlide(Model model,...){
    try {
        //invoke your webservice Here if success
        return  new ModelAndView("redirect:/anotherPage?success=true");

    } catch (HttpClientErrorException e) {
        //if failure
        return new ModelAndView("redirect:/anotherPage?success=false");
    }

}

or :

@RequestMapping(...)
public String deleteMySlide(Model model,...){
    try {
        //invoke your webservice Here if success
         model.addAttribute("message","sample success");
        return "redirect:/successPage");;

    } catch (HttpClientErrorException e) {
        //if failure
        model.addAttribute("message","sample failure");
        return "redirect:/failurePage");
    }

}

Upvotes: 0

sschrass
sschrass

Reputation: 7166

I would return a HTTP 204 OK in order to signal that the request has succeeded.

If you need to return a response body, in case the deletion has triggered something, I would use a HTTP 200 OK with a body attached.

Upvotes: 0

Related Questions