duanpanbo9476 2016-03-04 18:41
浏览 107
已采纳

REST API PUT请求更新资源

I want to update one of the resources I defined (user) to update his current status (online/offline) and/or location. Therefore I would use a PUT request like this:

PUT http://server/v1/users/12345

Payload

{
   "status": 0
}

to set the status offline for example.

Or it could be:

Payload

{
   "latitude": 100,
   "longitude": 100
}

The backend is based on Laravel/PHP and I respond to this request in my controller:

public function update(Request $request, $userReference) {

        // Get payload from request
        $bodyContent = json_decode($request->getContent(), true);
        $userReference = $userReference;

        // Update the user location
        $updateResponderLocationCommand = new UpdateResponderLocationCommand($bodyContent);
        $this->commandBus->execute($updateResponderLocationCommand);

        $response = [
            'userReference' => $userReference
        ];

        return $this->setStatusCode(201)->respond($response);
    }

This controller uses a command system I integrated that will trigger the task of doing the update.

My questions are, where I struggle:

  1. How do I do differentiate between the commands that should be executed. Right now, only the LocationUpdate is in this method. But I don't want to write a new update message just for the status update.
  2. So how does a request need to work properly? Can I still use the approach with PUT http://server/v1/user/100 and by having keywords in the payload and a select switch - approach within the controller a differentiation between tasks to be executed upon?
  3. Am I suppsoed to use PUT at all, when I only update a single component? I read that I should use POST instead?
  • 写回答

1条回答 默认 最新

  • doutangtan6386 2016-03-04 18:59
    关注
    1. Typically, the corresponding action would be sent through the routing mechanism. We would have an action property on your json object that corresponds to the function that should take place.

    Lets have a look at that:

    public function update(....){
        switch($request->get('action'))
        {
            case 'location':
                //execute the command here
                break;
            case 'status':
                //execute the command here
                break;
        }
    }
    

    In this way, we can make sure that our commands are dispatched according to our actions. This is the method that I prefer.

    1. Regarding the whole put v.s. post - there's no reason to do another write-up as this question thread here has the best breakdown and explanation by far.

    2. You should refer back to the link in #2. When creating a resource, you should use PUT. This makes sure that the request is idempotent and will not be duplicated (client's hung, server bottlenecked, etc).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法