Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API Design regarding DELETE method

I'm building a REST API. actually I understand the the common guide and rule.

But i have issue with DELETE method, because I need to send the data over the body in the request, which DELETE method will ignore the body.

If you asking what data that makes me send it over body in DELETE method, is a 'url' and some other parameter. Of course the 'url' has id in the database, so i can use DELETE with no problem, like DELETE https://api.example.com/content/url:url_id. But rather that pass the id, i chose to pass the url it self and some other param. my business logic and requirement force me to pass the url not the id in DELETE method.

so after reading, i find also some proxy blocking DELETE and PUT method. and also the HTML form only support GET and POST method.

i'm starting thinking that better to only use GET and POST in my REST API. so i can user POST for delete and object or resource like this:

POST /content/delete/url
Body :
    url : xxxx
    param1 : xxxx
    param2 : xxx

But in "REST API Design rulebook, O'reilly", page 18 said

"HTTP request methods should be used to indicate which CRUD function is performed"

The following anti-patterns exemplify what not to do:

GET /deleteUser?id=1234
GET /deleteUser/1234
POST /users/1234/delete

after searching and reading again, I came with some solution

  1. using X-HTTP-Method-Override

  2. using api method name like flicker(api.flickr.com/services/rest/?method=flickr.collections.getInfo) and mailchimp(api.mailchimp.com/1.3/?method=campaignDelete)

I think I like solution 1, to use 'X-HTTP-Method-Override'. What do you think?

Google seem to use X-HTTP-Method-Override, rever to this https://developers.google.com/gdata/docs/2.0/basics

Flicker and Mailchimp use method name, like in solution 2

like image 457
Ahmad Avatar asked Nov 24 '22 07:11

Ahmad


2 Answers

I know it is part of you business logic, but i would recommend you to rethink it or maybe try to use another solution instead of REST.

By doing stuff like the ones you mentioned, u will be breaking all the REST concepts and still not doing something good enough on your application.

I think the best solution in your case would be think on your business logic. Maybe it can be done without break REST.

If u think it cant be done, then i would recommend the first solution u listed. It feels less wrong.

Hope it helps.

like image 118
Paulo Henrique Avatar answered Nov 26 '22 21:11

Paulo Henrique


you CANNOT send body with a DELETE request. and it doesnt make sense!

RESTful would be

DELETE  http://www.plocal:3000/api/v1/content/page-1
DELETE  http://www.plocal:3000/api/v1/content/info-page
DELETE  http://www.plocal:3000/api/v1/content/1
DELETE  http://www.plocal:3000/api/v1/content/2

testcall with

curl -v http://www.plocal:3000/api/v1/content -X DELETE
like image 39
Tim Kretschmer Avatar answered Nov 26 '22 20:11

Tim Kretschmer