Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

http delete with REST

Tags:

rest

jersey

I am currently using Jersey Framework (JAX-RS implementation) for building RESTful Web Services. The Resource classes in the project have implemented the standard HTTP operations - GET,POST & DELETE. I am trying to figure out how to send request parameters from client to these methods.

For GET it would be in the query string(extract using @QueryParam) and POST would be name/value pair list (extract using @FormParam) sent in with the request body. I tested them using HTTPClient and worked fine. For DELETE operation, I am not finding any conclusive answers on the parameter type/format. Does DELETE operation receive parameters in the query string(extract using @QueryParam) or in the body(extract using @FormParam)?

In most DELETE examples on the web, I observe the use of @PathParam annotation for parameter extraction(this would be from the query string again).

Is this the correct way of passing parameters to the DELETE method? I just want to be careful here so that I am not violating any REST principles.

like image 987
Arun Avatar asked Oct 28 '10 17:10

Arun


People also ask

How do I delete data from REST API?

Use the sObject Rows resource to delete records. Specify the record ID and use the DELETE method of the resource to delete a record.

How do I use REST delete?

In RESTful APIs resources are typically deleted using the HTTP DELETE method. The resource that should be deleted is identified by the request URI. DELETE is an idempotent HTTP operation. Sending the same DELETE request multiple times should only alter the server state once.

Can HTTP delete contain body?

By this it seems optional whether you want to provide a body for a DELETE request. The RFC states that: A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.

What HTTP response for delete?

A successful response of DELETE requests SHOULD be an HTTP response code 200 (OK) if the response includes an entity describing the status. The status should be 202 (Accepted) if the action has been queued.


3 Answers

Yes, its up to you, but as I get REST ideology, DELETE URL should delete something that is returned by a GET URL request. For example, if

GET http://server/app/item/45678

returns item with id 45678,

DELETE http://server/app/item/45678

should delete it.

Thus, I think it is better to use PathParam than QueryParam, when QueryParam can be used to control some aspects of work.

DELETE http://server/app/item/45678?wipeData=true
like image 102
tuxSlayer Avatar answered Oct 04 '22 22:10

tuxSlayer


The DELETE method should use the URL to identify the resource to delete. This means you can use either path parameters or query parameters. Beyond that, there is no right and wrong way to construct an URL as far as REST is concerned.

like image 35
Darrel Miller Avatar answered Oct 05 '22 00:10

Darrel Miller


You can use like this

URL is http://yourapp/person/personid

@DELETE
@Path("/person/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response deletePerson(@PathParam("id") String id){
    Result result = new Result();
    try{
        persenService.deletePerson(id);
        result.setResponce("success"); 
    }
    catch (Exception e){
        result.setResponce("fail");
        e.printStackTrace();
    }
    return Response.status(200).entity(result).build();
}
like image 39
Srinivas Bheemreddy Avatar answered Oct 04 '22 23:10

Srinivas Bheemreddy