Search
  • +44-7459919437 (UK- WhatsApp & Direct Call) | +91-6383544892 (India - WhatsApp Only) | Email Id : vinothrwins@gmail.com
Vinoth Q.A Academy
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I
  • Home
  • Selenium Java Online Training
  • Self Paced Video Course
    • Selenium Course Curriculum
    • Cypress Course Curriculum
    • Playwright Course Curriculum
  • Tutorials
  • Demo Sites
    • Practice Automation
      • Demo Page Healthcare
      • Registration Form
      • Transaction Details
      • DropDown
      • Mouse Event
      • Keyboard Events
      • Alert and Popup
      • Multiple Windows
      • iFrames
      • Wait WebElement
      • WebTable
  • FAQS
  • About Me & Feedback
    • Placed Students Feedback
    • Online Training Feedback
    • LinkedIn Profile
    • TechTalk
  • Free YouTube Courses
    • Python for Automation
    • Free QA Video Courses
      • Manual Testing
      • Java For Automation
      • Selenium Webdriver
      • TestNG
      • Cucumber BDD
      • UFT(QTP) Automation
    • Free Data Science Courses
      • Artificial Intelligence for Beginners
      • Python For A.I
      • Python Pandas
      • Python NumPy
      • Mathematics for A.I

API Testing

  • What is an API?
  • Why is API Testing Important?
  • Advantages of API Testing
  • Disadvantages or Challenges of API Testing
  • Differences between API Testing, Unit Testing, and Integration Testing
  • Overview of XML 
  • Overview of JSON
  • Comparison between XML and JSON
  • What are API Methods?
  • What are HTTP Status Codes?
  • What is Web Service Testing?
  • Difference between API and WebService Testing
  • Types of APIs:
  • Request Headers and Parameters
  • Authentication and Authorization
  • API Keys for API Testing
  • OAuth Keys for API Testing
  • JWT (JSON Web Tokens) for API Testing
  • Creating Test Cases for API Functional Testing 
  • Advanced API Testing Techniques
  • Mocking and Stubbing APIs using postman api
  • Contract Testing for APIs
  • Overview of Popular API Testing Frameworks
  • API Testing Best Practices
View Categories
  • Home
  • Tutorials
  • API Testing
  • API Testing
  • JWT (JSON Web Tokens) for API Testing

JWT (JSON Web Tokens) for API Testing

JWT (JSON Web Tokens) for API Testing

JSON Web Tokens (JWT) are a popular method for securely transmitting information between parties as a JSON object. JWTs are particularly useful for API authentication and authorization, as they can be easily passed between a client and a server and verified quickly. Here’s an overview and an example to illustrate how JWTs work, especially in the context of API testing.

What is JWT? #

A JWT is a compact, URL-safe token that consists of three parts:

  1. Header: Contains metadata about the token, typically specifying the type of token (JWT) and the signing algorithm used, such as HMAC SHA256.
  2. Payload: Contains the claims, which are statements about an entity (typically, the user) and additional data. Claims can be predefined (like iss, exp, sub, etc.) or custom (such as role or username).
  3. Signature: Used to verify that the token was not altered. The signature is created by taking the encoded header, the encoded payload, a secret key, and the specified algorithm.

The JWT structure is as follows:

HEADER.PAYLOAD.SIGNATURE

Example: JWT in API Testing #

Let’s assume you have an API that requires users to be authenticated to access certain endpoints, like /user/profile. The authentication system uses JWT to issue tokens.

Step 1: User Logs In and Receives a JWT #

A user sends a login request with their credentials:

POST /api/login

Content-Type: application/json

{

  "username": "exampleuser",

  "password": "password123"

}

If the credentials are valid, the server responds with a JWT:

{

  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJleGFtcGxldXNlciIsInJvbGUiOiJ1c2VyIiwiaWF0IjoxNjkxNzMxNjAwLCJleHAiOjE2OTE3MzUyMDB9.ZJqcX-1XlIS4nApXFP1F3qz57MNlpsJ1RyZNLmESKKw"

}

This token is a JWT and will be used for subsequent requests to authenticated endpoints.

Step 2: Making an Authenticated Request #

To access the /user/profile endpoint, the client includes the JWT in the Authorization header:

GET /api/user/profile

Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJleGFtcGxldXNlciIsInJvbGUiOiJ1c2VyIiwiaWF0IjoxNjk1NjMxNjAwLCJleHAiOjE2OTU2MzUyMDB9.ZJqcX-1XlIS4nApXFP1F3qz57MNlpsJ1RyZNLmESKKw

The server receives the request and extracts the token. It then decodes the JWT, verifies the signature, and checks the claims (like expiration time). If the token is valid and not expired, the server processes the request and returns the user profile data.

Step 3: Decoding the JWT (For Testing Purposes) #

During API testing, you might need to decode the JWT to verify its contents. You can use online tools like jwt.io or libraries in various programming languages to decode the token and inspect the header and payload.

For example, decoding the example token might reveal:

Header:

{

  "alg": "HS256",

  "typ": "JWT"

}

Payload:

{

  "sub": "exampleuser",

  "role": "user",

  "iat": 1695631600,

  "exp": 1695635200

}

Signature: The signature part of the JWT would be verified using the secret key, ensuring the token hasn’t been tampered with.

Key Points in API Testing with JWT #

  • Token Expiry: Ensure that tokens expire as expected and cannot be reused indefinitely.
  • Authorization Checks: Test various roles and permissions by modifying the claims in the JWT.
  • Tampering: Attempt to tamper with the token to ensure the server correctly identifies and rejects invalid tokens.
  • Error Handling: Verify how the API handles expired or invalid tokens.

JWTs are a powerful tool for securing APIs, and understanding how to work with them is essential for effective API testing.

API Testing
What are your Feelings
Share This Article :
  • Facebook
  • X
  • LinkedIn
OAuth Keys for API TestingCreating Test Cases for API Functional Testing 
Table of Contents
  • What is JWT?
  • Example: JWT in API Testing
    • Step 1: User Logs In and Receives a JWT
    • Step 2: Making an Authenticated Request
    • Step 3: Decoding the JWT (For Testing Purposes)
  • Key Points in API Testing with JWT
© Copyright [2018-2025]. All Rights Reserved.