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

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-09-11 14:16:01
forward
1602 people have browsed it

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);
}
Copy after login

Output

The output of the above code is −

Array (
   [_token] => 367OQ9dozmWlnhu6sSs9IvHN7XWa6YKpSnnWrBXx
   [name] => Rasika Desai
   [email] => rasika@gmail.com
   [age] => 20
   [address] => Pune
)
Copy after login
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);
}
Copy after login

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] =>
)
Copy after login

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;
}
Copy after login

Output

The output of the above code is

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

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);
Copy after login

Output

The output of the above code is −

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

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Composer cannot install laravel
From 1970-01-01 08:00:00
0
0
0
Laravel Space/laravel-backup cannot be installed
From 1970-01-01 08:00:00
0
0
0
Laravel 5.1 Login laravel comes with it No more
From 1970-01-01 08:00:00
0
0
0
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template