How do I get HTTP Request body content in Laravel?
Solution 1:
Inside controller inject Request object. So if you want to access request body inside controller method 'foo' do the following:
public function foo(Request $request){
$bodyContent = $request->getContent();
}
Solution 2:
You can pass data as the third argument to call()
. Or, depending on your API, it's possible you may want to use the sixth parameter.
From the docs:
$this->call($method, $uri, $parameters, $files, $server, $content);
Solution 3:
I don't think you want the data from your Request, I think you want the data from your Response. The two are different. Also you should build your response correctly in your controller.
Looking at the class in edit #2, I would make it look like this:
class XmlController extends Controller
{
public function index()
{
$content = Request::all();
return Response::json($content);
}
}
Once you've gotten that far you should check the content of your response in your test case (use print_r if necessary), you should see the data inside.
More information on Laravel responses here:
http://laravel.com/docs/5.0/responses
Solution 4:
For those who are still getting blank response with $request->getContent()
, you can use:
$request->all()
e.g:
public function foo(Request $request){
$bodyContent = $request->all();
}