Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restful webservices....Put or post for a updating

I have a restful webservice for which iam writing a new method call. The purpose of the new method call is to revoke the status of a person to "NO".

All that I have to send in the request is a Person_Id for which the status needs to be changed to "NO".

Should I be using PUT or POST to do this?

If I use put, can I just send the person_id as a path parameter and not use any xml at all. (like : http://serverName/PersonServices/Person/123456) and in the service layer, i have my code like this.

    @PUT
    @Path("/Person/{person_Id}")
    @Consumes("application/xml")
    @Produces("application/xml")
    public JAXBElement<GetUsageTokenType> updateLicenseStatus(
            @PathParam("person_Id") final Long personId) {

            //code to change the status
     }

Or should I be using POST to do this... Am I right in saying that if I use POST, I need to send xml format?

like image 754
user1717230 Avatar asked Dec 07 '25 01:12

user1717230


1 Answers

If you look at Which HTTP methods match up to which CRUD methods? there shows the mapping

Create = PUT with a new URI
         POST to a base URI returning a newly created URI  
Read   = GET  
Update = PUT with an existing URI  
Delete = DELETE

If you read the HTTP RFC for each verb's definition you can see why...

In response to:

can I just send the person_id as a path parameter and not use any xml at all

then ideally your URL

http://serverName/PersonServices/Person/123456

should probably be

http://serverName/PersonServices/RevokePerson/123456

This will help for maintainability if you don't want to pass any XML/JSON/etc but whether human-readable URLs are a feature of RESTful services is a llloooonnnggg-running argument

As alluded to in the comments RESTful APIs should focus on content and not actions. See: Why does including an action verb in the URI in a REST implementation violate the protocol? for example.

So you'd pass a person to a URL and based on the HTTP verb the relevant CRUD action is taken. If you want to pass just an ID then you're going to have to guess what update to take when you PUT to the ID... unless you know that there's never going to be any other update ever ever ever.

like image 170
Paul D'Ambra Avatar answered Dec 08 '25 14:12

Paul D'Ambra