Pass X-xsrf-token when a POST request is made via postman

Source: Internet
Author: User
Tags postman interceptor
Preface Introduction

During this time, a project backend was laravel. Test through POSTMAN6 when writing API interfaces. However, after testing the form of the interface, Laravel comes with the CSRF authentication mechanism. This is embarrassing ...

So our purpose in using Postman is to test the POST request by XSRF verification. As an example of Laravel, Laravel will return to the browser's get request to write Xsrf-token in a cookie. So we need to take the xsrf-token from the cookie and send it out in the header of the POST request. To achieve cross-site request forgery verification that bypasses Laravel.

Installing the Postman Interceptor (Chrome extension feature)

Using postman If you make the data yourself by stitching the URL yourself, it is very troublesome. So choose Postman + Postman Interceptor, use Postman Interceptor can intercept the Web request, and upload to Postman History list, then we just need to modify the URL in the postman can be sent directly.

Laravel uses Chrome as an interceptor to record cookies, so install the postman's extensions on the Chrome browser first.

:https: //CHROME.GOOGLE.COM/WEBSTORE/DETAIL/POSTMAN-INTERCEPTOR/AICMKGPGAKDDGNAPHHHPLIIFPCFHICFO

Well...... Browse download install need FQ ...

Create an environment

We open the postman and click on the top right corner of the page to set the environment.

New Evnironment

Enter the new environment name and press "Add"

Select the newly created environment

send a GET request to get the XSRF token

Create a new request and add the following code to the Postman's Test label:

Pm.environment. Set (    // This is called    decodeuricomponent (Pm.cookies.get ("Xsrf-token" ))
The added code is executed when the request server returns successfully, so the cookie can be obtained and stored in the postman's environment variable for use upon request.

to access the GET request you just created, click Send, check if the XSRF token is in the environment variable

post comes with xsrf tokens

Laravel presets require an HTTP header with an XSRF token when publishing

Here's the code I tested:

// web.php Route::p ost (' posttest ', ' operatecontroller@posttest '); // operatecontroller.php  Public function $request {    var_dump(' Post test ');}
Add post tab and add XSRF token to header

The format is: {{"Environment variable"}}: The value of the environment variable used to get.

X-xsrf-token:{{xsrf-token}}

Click Send, Print successfully

Above

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.