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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 list的ad方法出错求解答
      • ¥20 关于mmap将物理空间映射到用户空间再落盘时使用O_DIRECT方式会提示bad address的问题如何解决?
      • ¥15 无人机px4下载环境编译出错,如何解决?
      • ¥15 您好,请问您的那个pcap转成pcd文件的matlab代码我下载了运行没问题,也生成了点云图,但是无法存储pcd文件,请问怎么保存导出pcd文件呀
      • ¥260 已有标注好的图像数据集,如何构建深度卷积网络训练分类
      • ¥20 matlab图像错误
      • ¥30 如何写通过uni.uploadFile上传图片到ASP后端接收的代码
      • ¥15 selenium模糊定位
      • ¥15 硬件断点的问题,真的被整无语了
      • ¥30 想入门网络编程求建议