Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

I have some resource- UserProfile

public UserProfile
{
   public string Email{get;set;}
   public string Password{get;set;}
}

I want to change Email and Password separatly (only one for user at same time). I have web api controller for example /api/user/123 that handle requests in RESTful style. Follow the RESTful style i should have one method PUT which update the resource, but i have two task that update the same resource api/user/123. I need to add some feature to PUT request body like {email:'[email protected]', changeType:'email'} or {password:'12345678',changeType:'password' } to write some if in my PUT method ? Or there is some other way to update my resource in RESTful style ?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

You have two options for updating email and password separately.

A) Don't use PUT, use POST

B) Create child resources for updating the individual elements, e.g.

PUT /api/user/123/email

And

PUT /api/user/123/password
share|improve this answer
    
Thanks, I've understood B), but I can't understand A) how I can use one method POST to separately update email and password ? – kolesso Feb 4 '14 at 14:41
    
You could use POST by putting the details in the body. However, B is more RESTful. – Mike Dunker Feb 4 '14 at 17:09
    
@MikeDunker More RESTful based on what criteria of REST. Creating micro resources such as those in example B can make a real mess of caching. There are pros and cons to both approaches. "RESTfulness" is not a particularly useful measure. – Darrel Miller Feb 4 '14 at 17:28
[HttpPut]
public HttpResponseMessage PutProduct(Product p)
{
    Product pro = _products.Find(pr => pr.Id == p.Id);

    if (pro == null)
        return new HttpResponseMessage(HttpStatusCode.NotFound);

    pro.Id = p.Id;
    pro.Name = p.Name;
    pro.Description = p.Description;

    return new HttpResponseMessage(HttpStatusCode.OK);
}
share|improve this answer
1  
An explaination is always better. – Jérémie Bertrand Sep 1 at 13:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.