Complete the code to set the Authorization header with a Bearer token in Postman.
pm.request.headers.add({ key: 'Authorization', value: 'Bearer [1]' });The Authorization header for Bearer tokens uses the keyword 'Bearer' followed by the token value. Here, 'token' is the correct variable holding the token.
Complete the test script to check if the response has a Bearer token in the Authorization header.
pm.test('Response has Bearer token', () => { pm.expect(pm.response.headers.get('Authorization')).to.include('[1]'); });
The Authorization header for Bearer tokens always includes the word 'Bearer'. This test checks that the header contains 'Bearer'.
Fix the error in this script that sets the Bearer token from environment variable 'authToken'.
pm.request.headers.upsert({ key: 'Authorization', value: 'Bearer [1]' });To get an environment variable in Postman scripts, use pm.environment.get('variableName'). This correctly fetches 'authToken'.
Fill both blanks to extract the Bearer token from the Authorization header and save it to an environment variable.
const authHeader = pm.response.headers.get('[1]'); const token = authHeader.split('[2]')[1].trim(); pm.environment.set('bearerToken', token);
The Authorization header key is 'Authorization'. The token is after the string 'Bearer ' (with a space), so splitting by 'Bearer ' extracts the token.
Fill all three blanks to write a test that verifies the Bearer token is present and not empty in the Authorization header.
pm.test('Bearer token is present and valid', () => { const authHeader = pm.response.headers.get('[1]'); pm.expect(authHeader).to.exist; pm.expect(authHeader).to.match(/^[2]\s+\S+$/); const token = authHeader.split('[3]')[1]; pm.expect(token.length).to.be.above(0); });
The header key is 'Authorization'. The regex checks the header starts with 'Bearer' followed by space and token. Splitting by 'Bearer ' extracts the token.