How to use HTTP in laravel to make requests

How to use HTTP in laravel to make requests

In this article let’s understand how to use HTTP in laravel to make requests.

Please note that this is only available for laravel 7.X versions. Its not available for previous versions.

So, laravel introduced this quick and easy to use api to make requests from any other third party. It provides various types of request and response handling.

To use HTTP make sure you install Guzzle Http Client. Install via composer using below command.

composer require guzzlehttp/guzzle

It will install the package and it’s dependencies.

Let’s checkout it’s syntax and working for different method types.

You can access the HTTP Client in your code by using the HTTP fa├žade. Add it to your file’s header.

use Illuminate\Support\Facades\Http;

GET

$response = Http::get('https://api.example.com/data');

$statusCode = $response->status();
$body = $response->body();

Set request as above and you can check response code and body from the output response parameter.

You can use the withQuery() method to add query parameters as below.

$response = Http::withQuery(['param1' => 'value1', 'param2' => 'value2'])
                ->get('https://api.example.com/data');

POST

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

Use the key-value pair to set post parameters in POST request as above.

DELETE

$response = Http::delete('https://api.example.com/resource/1');

Similarly use delete method to delete resource with given id.

Laravel HTTP provides many other functions such as response parameters, setting additional headers, throw exception and so on. Check few below methods.

Other methods

withHeaders()

To add headers :

$response = Http::withHeaders([
'Authorization' => 'Bearer your_token',
])->get('https://api.example.com/data');

throw()

To set exceptions:

$response = Http::get('https://api.example.com/data')->throw();

withToken

To directly add bearer token:

$response = Http::withToken('token')->post(/* ... */);

Response

The following techniques may be used check whether a response has a specific status code:

$response->ok() : bool;                  // 200 OK
$response->created() : bool;             // 201 Created
$response->accepted() : bool;            // 202 Accepted
$response->noContent() : bool;           // 204 No Content
$response->movedPermanently() : bool;    // 301 Moved Permanently
$response->found() : bool;               // 302 Found
$response->badRequest() : bool;          // 400 Bad Request
$response->unauthorized() : bool;        // 401 Unauthorized
$response->paymentRequired() : bool;     // 402 Payment Required
$response->forbidden() : bool;           // 403 Forbidden
$response->notFound() : bool;            // 404 Not Found
$response->requestTimeout() : bool;      // 408 Request Timeout
$response->conflict() : bool;            // 409 Conflict
$response->unprocessableEntity() : bool; // 422 Unprocessable Entity
$response->tooManyRequests() : bool;     // 429 Too Many Requests
$response->serverError() : bool;         // 500 Internal Server Error

That’s all on How to use HTTP in laravel to make requests. You can check Laravel official documentation for many other functionalities. Hope this finds you helpful.

Checkout more articles on Laravel here.