开发者

Update a model data using PUT

开发者 https://www.devze.com 2023-03-25 08:56 出处:网络
I have a user model, I am providing a RESTful API so the client can call to update the necessary data for a given user. The following is the code and 开发者_开发知识库also the curl command that I used

I have a user model, I am providing a RESTful API so the client can call to update the necessary data for a given user. The following is the code and 开发者_开发知识库also the curl command that I used to update the user data, but it didn't work anyhow. Is there anything wrong with my code/command?

And I have the following route setup

PUT     /user/{<\d+>id}/?                       Users.update

// Updates a single user.
public static void update(Long id) {
    // Fetch from user from DB
    User user = safeFindById(id);

    // Set new values
    user.edit("user", params.all());

    // Persist user
    user.validateAndSave();

    // return the user rendered based on template
    get(id);
}

Using Curl - add a new user

$ curl d '{"email":"admin@foo.com","password":"secret","firstname":"who","lastname":"is"}' -X POST http://localhost:9001/user/add

To update the user, both commands below didn't work

$ curl -H "Accept: application/json" -X PUT -d "firstname=test" http://localhost:9001/user/1

$ curl -H "Accept: application/json" -H "X-HTTP-Method-Override: PUT" -X POST -d "firstname=test" http://localhost:9001/user/1


Well without the complete route and a exception is difficult to analyze the problem. Furthermore I can't know how if your User-Object hast really a firstname and not a firstName. I would recommend to change the method:

public static void update(Long id, String firstname) {
    Logger.error(firstname);
    ....
}

So you could check if the error was in the request.

0

精彩评论

暂无评论...
验证码 换一张
取 消