如何在使用 Laravel 4 构建的 rest api 中从 PUT 请求中获取输入数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23472917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get input data from PUT request in a rest api built using Laravel 4
提问by Rajat Saxena
I am using the following request to post data to the REST api using PUT method:
我正在使用以下请求使用 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
But I don't know how to get the lat/lon data into the Restful Controller.I have tried
但我不知道如何将纬度/经度数据放入 Restful Controller.I 尝试过
Input::all()
which gives nullInput::json->all()
which also gives nullRequest::getContent()
which gives {"lat":56.87987,"lon":97.87678234} but I don't know how to parse that.
Input::all()
这给空Input::json->all()
这也给空Request::getContent()
这给出了 {"lat":56.87987,"lon":97.87678234} 但我不知道如何解析它。
Any help would be appreciated.Thanks
任何帮助将不胜感激。谢谢
回答by ollieread
Laravel is funny when posting JSON to it. It's a bit cheaty as it expects that the JSON data is part of a POST body query string, rather than the entire body is json.
Laravel 向其发布 JSON 时很有趣。这有点作弊,因为它期望 JSON 数据是 POST 主体查询字符串的一部分,而不是整个主体都是 json。
Instead of Input::json->all()
, use Request::json()->all()
.
而不是Input::json->all()
,使用Request::json()->all()
。
I actually recently built an API that accepts pure JSON requests for GET
, POST
, DELETE
, PUT
and PATCH
.
事实上,我最近建成接受纯JSON请求的API GET
,POST
,DELETE
,PUT
和PATCH
。
Example:
例子:
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();
}
Hope that helps.
希望有帮助。
回答by Rajat Saxena
Okay.I am now using this work around
好的,我现在正在使用这个解决方法
CURL.EXE -d 'lat=56.699857676' -X post "http://webservice.dev:8081/api/v1/interface/pF265UO3d68UNp0ID6hwclL88f7F5pz?_method=PUT&lat=56.9837487943&lon=78.8974982374"
I don't know whether this method is the right way of doing PUT request or is it safe or not.
我不知道这种方法是否是执行 PUT 请求的正确方法,或者它是否安全。
回答by Vit Kos
Had the same issue, solved it with placing this under my filters.php file
有同样的问题,通过将它放在我的filters.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;
}
});
After this you can get all the params via Input
在此之后,您可以通过以下方式获取所有参数 Input