Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blog post: automatic tokens for postman pre-request scripts #45

Open
james2doyle opened this issue May 25, 2022 · 0 comments
Open

Blog post: automatic tokens for postman pre-request scripts #45

james2doyle opened this issue May 25, 2022 · 0 comments

Comments

@james2doyle
Copy link
Owner

Use a URL that contains a JSON token value and set that dynamically per-request

pm.sendRequest({
    url: ['http://', pm.collectionVariables.get("baseUrl"), '/sanctum/csrf-cookie'].join(''),
    method: 'GET'
}, function (error, response, { cookies }) {
    if (!error && cookies) {
        pm.environment.set('xsrfToken', cookies.get('XSRF-TOKEN'))
        pm.sendRequest({
            url: ['http://', pm.collectionVariables.get("baseUrl"), '/token.json'].join(''),
            method: 'GET'
        }, function (error, response) {
            if (!error && response) {
                try {
                    pm.collectionVariables.set('authToken', response.json().token)
                } catch (err) {
                    console.error(err);
                }
            }
        })
    }
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant