Skip to content

Instantly share code, notes, and snippets.

@ethanstenis
Last active September 13, 2023 01:11
Show Gist options
  • Star 24 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ethanstenis/3cc78c1d097680ac7ef0 to your computer and use it in GitHub Desktop.
Save ethanstenis/3cc78c1d097680ac7ef0 to your computer and use it in GitHub Desktop.
How to make Postman work with POST/PUT requests in Laravel...
To make Postman work with POST/PUT requests...
https://laravel.com/docs/5.2/routing#csrf-x-csrf-token
In addition to checking for the CSRF token as a POST parameter, the Laravel VerifyCsrfToken middleware will also check for the X-CSRF-TOKEN request header.
1. Store the token in a "meta" tag at the top of your root view file (layouts/app.blade.php)...
<meta name="csrf-token" content="{{ csrf_token() }}">
** If using jQuery, you can now instruct it to include the token in all request headers.
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
2. Make sure to download Postman Interceptor (to sync Postman with the browser?) and turn it "on" in both the browser and Postman.
https://chrome.google.com/webstore/detail/postman-interceptor/aicmkgpgakddgnaphhhpliifpcfhicfo?hl=en
3. Do something in your app in the browser, open up the console and search in the head for the value of the csrf_token...
<meta name="csrf-token" content="cbpj1L7ym6fdPJhl5Fc0mH4MMU71gK1zatutgC3d">
4. Add a header within Postman...
X-CSRF-TOKEN cbpj1L7ym6fdPJhl5Fc0mH4MMU71gK1zatutgC3d
It should work now. OTHER POSSIBLE ERRORS...
"Class whatever\whatever\whatever\Auth not found"
-> Add a backslash to Auth to make it the root directory instead of relative
$bookmark = new Bookmark;
$bookmark->user_id = \Auth::user()->id;
$bookmark->link = $request->link;
$bookmark->save();
return $bookmark;
"Trying to get property of non-object"
-> This means \Auth::user() is returning null
-> It's not utilizing the 'auth' middleware in your routes.php.
-> Make sure the associated routes are included in either the 'web' middleware or 'auth' middleware (or both)
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/home', 'HomeController@index');
Route::resource('users', 'UsersController', ['except' => [
'create', 'edit', 'store'
]]);
Route::resource('bookmarks', 'BookmarksController', ['except' => [
'create', 'edit'
]]);
Route::resource('tags', 'TagsController', ['except' => [
'create', 'edit'
]]);
Route::group(['middleware' => 'auth'], function() {
Route::resource('bookmarks', 'BookmarksController', ['only' => [
'store', 'create', 'destroy'
]]);
});
});
@basanta123
Copy link

I have done from step 1 to step 4 but not working error verify csrf token in post request

@shemul
Copy link

shemul commented Mar 17, 2017

thanks it works !

@Alishoaib
Copy link

thank you so much it worked for me

@kenmasters
Copy link

It worked for me also, Thanks :)

@Alishoaib
Copy link

it works for me but only for get request
for post request its not working :(

@nageennayak11
Copy link

great, work for me too.
thanks alot.

@rohitsSpace
Copy link

it's not working for me, neither in get nor in post-call

@codingenchanter725
Copy link

Thank you very much
learnt a lot.

@JuniorTak
Copy link

Not working for me 👎

@joshmakar
Copy link

This didn't work for me until I went into the Interceptor debugger in Postman and selected "Capture cookies".

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment