【发布时间】:2015-04-05 12:38:55
【问题描述】:
我有一个使用这个库创建的 codeigniter rest api: https://github.com/chriskacerguis/codeigniter-restserver
对于我的 POST 方法,一旦成功创建资源,我想包含一个 location 标头,其中包含一个 URI 以获取新创建/更新的资源. 我不清楚如何做到这一点。 有没有人尝试过类似的东西?
到目前为止,我只是将已创建内容的详细信息添加到响应正文中……但我最终希望将 GET url 添加为位置标头。
到目前为止,这是我的逻辑:
public function widget_post()
{
$new_widget_data = array(
'location' =>$this->post('location'),
'name' => $this->post('name'),
'cost' => $this->post('cost')
);
// validate post data
if (!$this->isvalidpost($new_widget_data)) {
$this->response("Invalid data",400);
}
// add to database
$res = $this->widget_model->notify_servers($new_widget_data);
$new_widget_data['results']=$res;
if ($res) {
//append GET url to return value
$new_widget_data['GET']=base_url()."/index.php/widgets/widget/".$new_widget_data['name'];
$this->response($new_widget_data,201); //HTTP Created 201
} else {
$this->response("Unable to process request",500); //HTTP Internal server error
}
任何建议将不胜感激。谢谢。
【问题讨论】:
标签: php rest http-headers codeigniter-2 codeigniter-restserver