Home  >  Article  >  Backend Development  >  How to get the content of HTTP request body in Laravel?

How to get the content of HTTP request body in Laravel?

WBOY
WBOYforward
2023-09-11 13:49:011508browse

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 -

How to get the content of HTTP request body in Laravel?

To get all the details from the HTTP request you can do as follows −

Example 1

is translated as:

Example 1

Using $request->all() method

Enter the following details into the form below:

How to get the content of HTTP request body in Laravel?

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);
}

Output

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

Example 2

is:

Example 2

Using $request->collect() method.

This method will return the data as a collection.

public function validateform(Request $request) {
   $input = $request->collect();
   print_r($input);
}

Output

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] =>
)

Example 3

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;
}

Output

The output of the above code is

_token=367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx&name=Rasika+Desai&email=rasika%40gmail.com&age=20&address=Pune

Example 4

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);

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete