How to make a post request in laravel?

Member

by brandy , in category: PHP Frameworks , 24 days ago

How to make a post request in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , 23 days ago

@brandy 

To make a POST request in Laravel, you can use the post method of the IlluminateSupportFacadesHttp facade. Here is an example of how you can make a POST request in Laravel:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
use IlluminateSupportFacadesHttp;

$response = Http::post('https://example.com/api/post-endpoint', [
    'key1' => 'value1',
    'key2' => 'value2',
]);

// You can access the response body like this
$response->json();

// You can also get the status code of the response
$response->status();


In the example above, we are making a POST request to https://example.com/api/post-endpoint with some data in the request body. You can replace the URL and data with your own values.


Remember to add the following line at the top of your PHP file to use the Http facade:

1
use IlluminateSupportFacadesHttp;


Also, don't forget to install Guzzle HTTP client by running the following command in your terminal:

1
composer require guzzlehttp/guzzle


That's it! You have successfully made a POST request in Laravel.