如何在使用Laravel 4构建的rest api中从PUT请求中获取输入数据


How to get input data from PUT request in a rest api built using Laravel 4

我使用以下请求使用PUT方法将数据发布到REST api:

CURL.EXE -i -H "Content-Type: application/json" -X put -d '{"lat":56.87987,"lon":97.87678234}' http://webservice.dev:8081/api/v1/interface/123

但是我不知道如何将经纬度数据放入Restful控制器中。我试过了

  • Input::all()给出null
  • Input::json->all()也给出null
  • Request::getContent()给出{"lat":56.87987,"lon":97.87678234},但我不知道如何解析。

任何帮助都会很感激。由于

Laravel在向它发送JSON时很有趣。这有点不可靠,因为它期望JSON数据是POST主体查询字符串的一部分,而不是整个主体都是JSON。

Request::json()->all()代替Input::json->all()

我实际上最近构建了一个API,可以接受GET, POST, DELETE, PUTPATCH的纯JSON请求。

的例子:

private function parseRequest() {
    $query = Request::query();
    if(!empty($query['includes'])) {
        $this->includes = explode(',', $query['includes']);
        unset($query['includes']);
    }
    if(!empty($query['page']) && $query['page'] > 0) {
        $this->page = $query['page'];
        unset($query['page']);
    }
    if(!empty($query['count']) && $query['count'] > 0) {
        $this->count = $query['count'];
        unset($query['count']);
    }
    if(!empty($query['token'])) {
        $this->token = $query['token'];
        unset($query['token']);
    }
    $this->query = $query;
    $this->parameters = Request::json()->all();
    $this->route = Route::current();
}

希望对你有帮助。

好的。我现在在

周围使用这个工作
CURL.EXE -d 'lat=56.699857676' -X post "http://webservice.dev:8081/api/v1/interface/pF265UO3d68UNp0ID6hwclL88f7F5pz?_method=PUT&lat=56.9837487943&lon=78.8974982374"

我不知道这个方法是做PUT请求的正确方式还是安全的。

有同样的问题,解决它与放置在我的过滤器。php文件

App::before(function($request)
{
    if (!App::runningInConsole())
    {
      header('Access-Control-Allow-Origin', '*');
      header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE');
      header('Access-Control-Allow-Headers', 'Origin, Content-Type, Accept, Client, Authorization');
      exit;
  }
});
之后你可以通过Input
获取所有的参数