使用PUT更新模型数据

时间:2011-08-04 06:13:41

标签: rest curl model playframework

我有一个用户模型,我提供RESTful API,因此客户端可以调用以更新给定用户的必要数据。以下是我用来更新用户数据的代码和curl命令,但无论如何它都无法正常工作。我的代码/命令有什么问题吗?

我有以下路线设置

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);
}

使用Curl - 添加新用户

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

要更新用户,以下两个命令都不起作用

$ 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

1 个答案:

答案 0 :(得分:0)

没有完整的路线,例外很难分析问题。此外,我不知道你的用户对象是如何真正的名字而不是firstName。我建议改变方法:

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

因此,您可以检查错误是否在请求中。