CORS Testing Examples with Postman
Introduction to CORS Testing
Cross-Origin Resource Sharing (CORS) is a security feature implemented by browsers to restrict web pages from making requests to a different domain than the one that served the web page. When developing APIs, it’s crucial to configure CORS correctly to ensure that your resources can be accessed by authorized domains. In this article, we present three practical examples of testing CORS configurations using Postman.
Example 1: Testing Basic CORS Configuration
Context
In this example, we’ll test a simple API that has CORS enabled for a specific origin, allowing requests only from that origin.
To verify that the CORS settings are functioning as expected, we will attempt a request from a different origin and observe the response.
Example
- Open Postman and create a new request.
- Set the request method to
GET. - Enter the API endpoint URL (e.g.,
https://api.example.com/data). Go to the
Headerstab and add a new header:- Key:
Origin - Value:
https://unauthorized-origin.com
- Key:
- Click
Sendto execute the request. Observe the response:
- If the CORS configuration is correct, you should receive a response indicating that the request is forbidden (HTTP status code 403).
Notes
- You can replace
https://unauthorized-origin.comwith any origin that is not authorized to access the API. - This test confirms that the API does not allow requests from unauthorized origins.
Example 2: Testing CORS with Allowed Origin
Context
In this example, we will test an API that allows requests from a specific origin. We will use Postman to send a request from that authorized origin and check the response headers for CORS.
Example
- Create a new request in Postman and set it to
GET. - Enter the API endpoint URL (e.g.,
https://api.example.com/data). In the
Headerstab, add the following header:- Key:
Origin - Value:
https://authorized-origin.com
- Key:
- Click
Sendto execute the request. Check the response headers:
- Look for
Access-Control-Allow-Originand ensure it includeshttps://authorized-origin.com. - The response should also return a successful status code (e.g., 200).
- Look for
Notes
- Adjust the
Originheader value to match the allowed origin configured in your API. - This test ensures that the API correctly permits requests from authorized origins.
Example 3: Testing Preflight Requests
Context
CORS preflight requests are sent by browsers before making certain types of requests (like PUT or DELETE). This example demonstrates how to simulate a preflight request using Postman to check if the server handles it correctly.
Example
- Create a new request in Postman and set it to
OPTIONS. - Enter the API endpoint URL (e.g.,
https://api.example.com/data). In the
Headerstab, add the following headers:- Key:
Origin - Value:
https://authorized-origin.com - Key:
Access-Control-Request-Method - Value:
POST
- Key:
- Click
Sendto execute the request. Examine the response:
- Verify that the
Access-Control-Allow-Methodsheader includesPOST(or the method you specified). - Ensure the response has a status code of 200 or 204 for successful preflight checks.
- Verify that the
Notes
- This example is crucial for testing APIs that require preflight checks before processing complex requests.
- You may need to adjust the
Access-Control-Request-Methodheader based on your API’s capabilities.
By following these examples of testing CORS configurations using Postman, developers can ensure that their APIs are secure and function correctly across different origins.
Related Topics
CORS in Flask API: 3 Practical Examples
Examples of Handling CORS Errors in Front-End Applications
Examples of CORS with Node.js and Koa framework
CORS: Practical Examples with Developer Tools
CORS Testing Examples with Postman
CORS in PHP Applications: 3 Examples
Explore More Implementing CORS in APIs
Discover more examples and insights in this category.
View All Implementing CORS in APIs