Understanding Cross-Origin Resource Sharing and how it works with Mock API Builder
CORS (Cross-Origin Resource Sharing) is a security feature implemented by web browsers to control how web pages in one domain can request resources from a different domain. It prevents malicious websites from accessing data from other sites without permission.
When your frontend application (e.g., localhost:3000) tries to call an API on a different origin (e.g., mockapibuilder.io/api/your-project), the browser blocks the request by default unless the API server explicitly allows it.
Example: A React app running on http://localhost:3000 making a fetch request to https://mockapibuilder.io/api/your-project/users is a cross-origin request.
Prevents unauthorized access to APIs from untrusted websites
Enables controlled data sharing between different origins
Enforced by browsers, not by servers or backend code
Mock API Builder automatically handles CORS for you. All mock APIs are configured to accept requests from any origin, so you can focus on development without CORS headaches.
Every response from Mock API Builder includes these CORS headers:
Access-Control-Allow-Origin: *Allows requests from any domain (localhost, your-site.com, etc.)
Access-Control-Allow-Methods: GET, POST, PUT, PATCH, DELETE, OPTIONSPermits all standard HTTP methods
Access-Control-Allow-Headers: Content-Type, AuthorizationAllows common headers like authentication tokens and content types
Access-Control-Max-Age: 86400Browsers can cache preflight requests for 24 hours
For simple GET requests, the browser sends the request and checks the response headers:
For complex requests (POST, PUT, DELETE, or custom headers), browsers send a preflight OPTIONS request first:
Note: Mock API Builder automatically responds to OPTIONS requests, so preflight checks always succeed.
Open your browser's DevTools to inspect CORS headers:
Access-Control-Allow-Origin is presentResponse Headers:
access-control-allow-origin: *
access-control-allow-methods: GET, POST, PUT, PATCH, DELETE, OPTIONS
access-control-allow-headers: Content-Type, Authorization
content-type: application/json
Error Message:
Solution:
Symptoms: POST/PUT/DELETE requests fail, but GET works
Solution:
Content-Type: application/jsonExplanation: CORS is a browser security feature. API clients like Postman bypass CORS entirely.
Solution:
When: You need to send cookies or authentication with requests
Solution: Add credentials option to fetch:
Note: Mock API Builder uses token-based auth (not cookies), so credentials are typically not needed.
Mock API Builder uses permissive CORS (*) for development convenience. When building your real production API, you should be more restrictive:
Instead of *, specify your actual domain(s)
Only allow methods your API actually uses
Only allow headers you need
Only if you need to send cookies/credentials (cannot use with * origin)
*