DEV Community

Cover image for Acessando rotas protegidas CSRF ou autenticadas do Laravel com o Postman
Marcello Gonçalves
Marcello Gonçalves

Posted on

2

Acessando rotas protegidas CSRF ou autenticadas do Laravel com o Postman

As vezes precisamos acessar rotas autenticadas ou rotas protegidas com o CSRF com o Postman para fazermos testes, abaixo vou mostrar como conseguimos fazer o login e pegar o valor do token sem precisar excluir URIs da proteção CSRF .

Lembrando que o postman é para testes de api, mas se por algum motivo precisarem acessar rotas que não sejam de api segue a dica.

1 - Criar o ambiente no postman

Environment name: Meu projeto
key: url 
value: http://127.0.0.1:8000/
Enter fullscreen mode Exit fullscreen mode

Environment postamn

2 - Agora você pode usar o endpoint com a variável criada no meu caso url , então ficaria assim: {{url}}/login

3 - Adicione o código abaixo na seção Tests para criar a variável chamada xsrf-token

var xsrfCookie = postman.getResponseCookie("XSRF-TOKEN");
var xsrfToken = decodeURIComponent(pm.cookies.get("XSRF-TOKEN"));
pm.environment.set("xsrf-token", xsrfToken);
Enter fullscreen mode Exit fullscreen mode

postman tests

4 - Na seção Headers adiciona as seguintes variáveis

Content-Type:application/x-www-form-urlencoded
X-XSRF-TOKEN:{{xsrf-token}}
Accept:application/json
Enter fullscreen mode Exit fullscreen mode

postman headers

5 - Agora você já pode enviar a solicitação POST para efetuar o login, não esqueçam de colocar no Body os campos email e password

postman auth

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs