Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Postman for Laravel $_POST request

Tags:

How can I try sending a post request to a Laravel app with Postman?

Normally Laravel has a csrf_token that we have to pass with a POST/PUT request. How can I get and send this value in Postman? Is it even possible without turning off the CSRF protection?

like image 393
MisterCat Avatar asked Feb 01 '16 18:02

MisterCat


People also ask

How can I send API request in laravel Postman?

Select the Body tab on postman and then choose x-www-form-urlencoded. 3. Copy the token and paste in postman as the value of the key named _token.

Can Postman receive post requests?

The Postman app has a built-in proxy that can capture HTTP and HTTPS traffic. Here's how it works: The Postman app listens for any calls made by a client app or device using the proxy. The Postman proxy captures the request and forwards it to the server.

What is Postman in laravel?

This package allows you to automatically generate a Postman collection based on your API routes. It also provides basic configuration and support for bearer auth tokens and basic auth for routes behind an auth middleware.


2 Answers

Edit:

Ah wait, I misread the question. You want to do it without turning off the CSRF protection? Like Bharat Geleda said: You can make a route that returns only the token and manually copy it in a _token field in postman.

But I would recommend excluding your api calls from the CSRF protection like below, and addin some sort of API authentication later.

Which version of laravel are you running?

Laravel 5.2 and up:

Since 5.2 the CSRF token is only required on routes with web middleware. So put your api routes outside the group with web middleware.

See the "The Default Routes File" heading in the documentation for more info.

Laravel 5.1 and 5.2:

You can exclude routes which should not have CSRF protection in the VerifyCsrfToken middleware like this:

class VerifyCsrfToken extends BaseVerifier {     /**      * The URIs that should be excluded from CSRF verification.      *      * @var array      */     protected $except = [         'api/*',     ]; } 

See the "Excluding URIs From CSRF Protection" heading documentation for more info.

like image 57
Björn Avatar answered Sep 23 '22 23:09

Björn


If you store your sessions in Cookies, you can grab the Cookie from an auth request in Developer Tools.

enter image description here

Copy and paste that Cookie in the Header of your POSTMAN or Paw requests.

enter image description here

This approach allows you to limit your API testing to your current session.

like image 40
Brian Fegter Avatar answered Sep 25 '22 23:09

Brian Fegter