Bird
Raised Fist0
Postmantesting~20 mins

Bearer token in Postman - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Bearer Token Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding Bearer Token Usage in API Requests

Which statement best describes the purpose of a Bearer token in API testing?

AIt is a token that encrypts the entire API request body for security.
BIt is a token used to authenticate API requests by including it in the Authorization header.
CIt is a token that specifies the API endpoint URL to be accessed.
DIt is a token that logs the user out of the API session automatically.
Attempts:
2 left
💡 Hint

Think about how APIs verify who is making the request.

Predict Output
intermediate
2:00remaining
What is the result of this Postman test script?

Given this Postman test script, what will be the value of pm.environment.get('authStatus') after running?

Postman
pm.test('Check status code', function () {
    pm.response.to.have.status(200);
    if (pm.response.code === 200) {
        pm.environment.set('authStatus', 'success');
    } else {
        pm.environment.set('authStatus', 'fail');
    }
});
A"success" if response status is 200, otherwise "fail"
B"fail" always because pm.response.code is undefined
C"success" always regardless of response status
DThrows an error because pm.environment.set is not a function
Attempts:
2 left
💡 Hint

Check how the script sets environment variables based on response code.

assertion
advanced
2:00remaining
Choose the correct assertion to verify Bearer token presence

Which Postman test assertion correctly checks that the Authorization header contains a Bearer token?

Apm.test('Bearer token present', () => pm.request.headers.has('Authorization') && pm.request.headers.get('Authorization').includes('Bearer'));
Bpm.test('Bearer token present', () => pm.response.headers.has('Authorization') && pm.response.headers.get('Authorization').startsWith('Bearer '));
Cpm.test('Bearer token present', () => pm.request.headers.get('Authorization').startsWith('Bearer '));
Dpm.test('Bearer token present', () => pm.response.headers.get('Authorization').includes('Bearer'));
Attempts:
2 left
💡 Hint

Remember that the Bearer token is sent in the request headers, not response headers.

🔧 Debug
advanced
2:00remaining
Identify the error in this Bearer token test script

What error will this Postman test script produce?

Postman
pm.test('Check Bearer token', () => {
    const authHeader = pm.request.headers.get('Authorization');
    pm.expect(authHeader).to.match(/^Bearer\s\w+$/);
});
ANo error, test passes always
BSyntaxError due to incorrect regex escape sequences
CTypeError because pm.request.headers.get is not a function
DAssertionError if Authorization header is missing or malformed
Attempts:
2 left
💡 Hint

Consider what happens if the Authorization header is missing or empty.

framework
expert
2:00remaining
Best practice for securely handling Bearer tokens in automated tests

Which approach is the most secure and maintainable way to handle Bearer tokens in automated API tests using Postman?

AStore the Bearer token in an environment variable and reference it in the Authorization header.
BGenerate a new Bearer token manually before each test run and paste it into the headers.
CInclude the Bearer token in the request body as plain text for easier access.
DHardcode the Bearer token directly in each request's Authorization header.
Attempts:
2 left
💡 Hint

Think about reusability and security of sensitive data in tests.

Practice

(1/5)
1. What is the correct way to include a Bearer token in a Postman request header?
easy
A. Add a query parameter named 'token' with the token value
B. Set the Authorization header to 'Bearer <token>'
C. Include the token in the request body as JSON
D. Set a cookie named 'Bearer' with the token value

Solution

  1. Step 1: Understand Bearer token usage in headers

    Bearer tokens are sent in the Authorization header to prove identity.
  2. Step 2: Identify correct header format

    The header must be 'Authorization: Bearer <token>' exactly.
  3. Final Answer:

    Set the Authorization header to 'Bearer <token>' -> Option B
  4. Quick Check:

    Authorization header = Bearer token [OK]
Hint: Always use Authorization header with 'Bearer ' prefix [OK]
Common Mistakes:
  • Putting token in query parameters instead of header
  • Sending token in request body instead of header
  • Using cookie instead of Authorization header
2. Which of the following is the correct syntax to add a Bearer token in Postman headers?
easy
A. "Auth": "Bearer <token>"
B. "Authorization": "Token <token>"
C. "Authorization": "Bearer <token>"
D. "Authorization": "Basic <token>"

Solution

  1. Step 1: Recall correct header key and value format

    The header key must be 'Authorization' and the value must start with 'Bearer '.
  2. Step 2: Match the exact syntax

    Only "Authorization": "Bearer <token>" uses 'Authorization' and 'Bearer <token>' correctly.
  3. Final Answer:

    "Authorization": "Bearer <token>" -> Option C
  4. Quick Check:

    Authorization = Bearer token syntax [OK]
Hint: Remember header key is 'Authorization' and value starts with 'Bearer ' [OK]
Common Mistakes:
  • Using 'Token' instead of 'Bearer' prefix
  • Using 'Auth' instead of 'Authorization' header
  • Confusing 'Basic' auth with Bearer token
3. Given this Postman test script snippet, what will be the value of the Authorization header sent?
pm.request.headers.add({key: 'Authorization', value: `Bearer ${pm.environment.get('token')}`});
medium
A. Authorization: Bearer <token_value_from_environment>
B. Authorization: Token <token_value_from_environment>
C. Authorization: Bearer undefined
D. Authorization: Basic <token_value_from_environment>

Solution

  1. Step 1: Understand the code usage of environment variable

    The code uses pm.environment.get('token') to get the token value from environment variables.
  2. Step 2: Analyze the header value construction

    The header value is 'Bearer ' plus the token value from environment, so it will be 'Bearer <token_value_from_environment>'.
  3. Final Answer:

    Authorization: Bearer <token_value_from_environment> -> Option A
  4. Quick Check:

    Header value = 'Bearer ' + environment token [OK]
Hint: Check environment variable usage inside template literals [OK]
Common Mistakes:
  • Assuming token is 'undefined' if environment variable missing
  • Using 'Token' instead of 'Bearer' prefix
  • Confusing Basic auth with Bearer token
4. You added a Bearer token in Postman but the API returns 401 Unauthorized. What is the most likely mistake?
medium
A. The Authorization header is missing the 'Bearer ' prefix
B. The token is placed in the request body instead of headers
C. The Content-Type header is set to 'application/json'
D. The token is expired or invalid

Solution

  1. Step 1: Check common causes of 401 Unauthorized with Bearer tokens

    401 usually means token is missing, malformed, or invalid/expired.
  2. Step 2: Identify the most likely cause given the token is added

    If the token is added correctly but still 401, it is likely expired or invalid.
  3. Final Answer:

    The token is expired or invalid -> Option D
  4. Quick Check:

    401 Unauthorized often means invalid token [OK]
Hint: Check token validity if 401 despite correct header [OK]
Common Mistakes:
  • Forgetting 'Bearer ' prefix in Authorization header
  • Placing token in body instead of header
  • Assuming Content-Type affects authorization
5. You want to automate testing of an API that requires a Bearer token which expires every hour. Which approach is best to handle this in Postman?
hard
A. Use a Pre-request Script to fetch a new token and set it dynamically before each request
B. Manually update the token in environment variables before each test run
C. Hardcode the token in the Authorization header and ignore expiration
D. Remove the Authorization header to avoid token expiration errors

Solution

  1. Step 1: Understand token expiration and automation needs

    Since the token expires hourly, manual updates are inefficient and error-prone.
  2. Step 2: Choose dynamic token fetching in Pre-request Script

    Using a Pre-request Script to get a fresh token before each request automates the process and avoids failures.
  3. Final Answer:

    Use a Pre-request Script to fetch a new token and set it dynamically before each request -> Option A
  4. Quick Check:

    Automate token refresh with Pre-request Script [OK]
Hint: Automate token refresh with Pre-request Script [OK]
Common Mistakes:
  • Manually updating tokens wastes time and causes errors
  • Hardcoding tokens ignores expiration and causes failures
  • Removing Authorization header breaks authentication