Home > Article > Backend Development > How to get the content of HTTP request body in Laravel?
To get the details of the HTTP request you need to make use of the class Illuminate\Http\Request.
Using the above classes you will be able to get input, cookies and files from HTTP requests. Now consider the following form -
To get all the details from the HTTP request you can do as follows −
Using $request->all() method
Enter the following details into the form below:
Once you submit it will retrieve all the input data and return an array with data.
public function validateform(Request $request) { $input = $request->all(); print_r($input); }
The output of the above code is −
Array ( [_token] => 367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune )The Chinese translation of
Using $request->collect() method.
This method will return the data as a collection.
public function validateform(Request $request) { $input = $request->collect(); print_r($input); }
The output of the above code is −
Illuminate\Support\Collection Object ( [items:protected] => Array( [_token] => 367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx [name] => Rasika Desai [email] => rasika@gmail.com [age] => 20 [address] => Pune ) [escapeWhenCastingToString:protected] => )
Use $request->getContent() method.
This method will output as a URL query string, and the data is passed in the form of key/value pairs.
public function validateform(Request $request) { $input = $request->getContent(); echo $input; }
The output of the above code is
_token=367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx&name=Rasika+Desai&email=rasika%40gmail.com&age=20&address=Pune
Use php://input
This will return data from the input fields in the URL query string.
$data = file_get_contents('php://input'); print_r($data);
The output of the above code is −
_token=367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx&name=Rasika+Desai&email=rasika%40gmail.com&age=20&address=Pune
The above is the detailed content of How to get the content of HTTP request body in Laravel?. For more information, please follow other related articles on the PHP Chinese website!