在Laravel中存储带有资源控制器的JSON
我正在致力于Laravel的一个安静的API解决方案。 我的问题是我无法让我的商店功能接受JSON。 在我的商店功能,我有以下代码(基于本教程http://code.tutsplus.com/tutorials/laravel-4-a-start-at-a-restful-api-updated--net-29785):
public function store()
{
$url = new Url;
$url->url = Request::get('url');
$url->description = Request::get('description');
$url->save();
return Response::json(array(
'error' => false,
'message' => 'test',
'urls' => $url->toArray()),
200
);
}
当我发送像这样的数据时:
curl -d "url=test&description=testing" localhost/api/v1/url
它按预期工作,并将记录插入到我的数据库中。
但是,当我尝试发布一些像这样的JSON
curl -d '{"url":"test","description":"test"}' localhost/api/v1/url
我甚至无法使用Request :: get('url')返回数据。 我已经在这里读过,即Input :: all()或Input :: json() - > all()可以工作,但我仍然无法返回发布的数据。 任何想法如何当它被张贴为JSON时访问网址和说明?
你没有正确地使用curl来发布json,试试这个:
curl -X POST -H "Content-Type: application/json" -d '{"url":"test","description":"test"}' localhost/api/v1/url
如果您使用的是Windows客户端,则可以使用Chrome插件Postman(http://www.getpostman.com/)。
将其配置为:
1)POST
2)原始(并粘贴你的JSON在框中)
3)头部:Content-Type值:application / json
为了做你的测试,你可以使用这样一个简单的路由器:
Route::any('test', function() {
Log::info(Input::all());
});
并执行
php artisan tail
查看发布后会发生什么。
链接地址: http://www.djcxy.com/p/8553.html