Skip to content

Let web pages call a project

Before you begin

Role
Admin or higher to change the setting and Viewer or higher to check browser access.

A page in a browser can read a response from another address only when the response allows it, which is called CORS. A new project allows every page, so a front end on http://localhost:3000 can call https://orders-api.mockhub.dev with no setup. Change it when the page sends cookies, or when you want to test how your app handles a refusal.

  1. Open the project’s Settings tab.

  2. Go to Responses and logging, and find Browser access (CORS).

  3. Select one choice:

    Choice What the project answers
    Any website Every page, with Access-Control-Allow-Origin: *. Pages cannot send cookies to the project.
    Only these origins Only the pages you list, which may send cookies.
    Off Nothing: MockHub adds no CORS headers, and your mocks set their own.
  4. For Only these origins, type each page’s origin into Origins allowed to call this project, such as http://localhost:5173, and press Enter. At least one is needed: saving an empty list shows “Add at least one origin, or choose another option.”

  5. Select Save settings.

An origin is the scheme, host and port of the page, exactly as the browser sends it in the Origin request header: http://localhost:5173 is not http://localhost:3000, https://localhost:5173 or http://127.0.0.1:5173. MockHub stores it in lowercase, without a trailing / and without the default port, so HTTPS://App.Example.com:443/ is stored as https://app.example.com. A project lists at most 20 origins.

MockHub adds CORS headers only to a request that carries an Origin header, which browsers send and curl and test runners usually do not.

Header Any website Only these origins, listed page
Access-Control-Allow-Origin * The page’s origin
Access-Control-Allow-Credentials Not sent true
Access-Control-Expose-Headers The response’s own headers, by name The same
Vary Origin Origin

Access-Control-Expose-Headers lets the page’s code read headers such as X-Total-Count, Link or Retry-After, which a browser otherwise hides.

The headers are added to every answer the project gives once the request reaches it: a mock’s answer, an answer recorded from your real API, the 404 when no mock matched, and MockHub’s own refusals, such as 402, 429 or 502. The page can therefore read the real status instead of a CORS error.

Before a request with a JSON body, an Authorization header or a method such as PUT or DELETE, the browser sends an OPTIONS request to the same address, called a preflight. MockHub answers it with 204, allowing the method and headers the browser asked for, and lets the browser reuse the answer for ten minutes.

These answers are not stored in the request logs and do not count against your request allowance.

To answer preflights yourself, create a mock whose HTTP method is OPTIONS. While the project has an active OPTIONS mock, preflights go to your mocks like any other request, and MockHub answers only the ones no mock matched. A mock whose method is Any method does not answer preflights unless it compares the Access-Control-Request-Method header itself. In Record everything your mocks do not run, so MockHub answers every preflight and none reaches your real API.

When curl works and the browser reports a CORS error, check the project before changing it. Anyone who can see the project can do this, and it changes nothing.

  1. Open the project’s Settings tab, and select Check browser access beside Browser access (CORS).

  2. Enter the Page origin from the failing request’s Origin header in your browser’s network panel, such as http://localhost:5173.

  3. Choose the address your page calls and type the path and query, such as /orders/42?view=summary.

  4. Choose the Method and Content-Type, and say whether the app sends an explicit Authorization header and whether it sends credentials (credentials: 'include' or withCredentials). Name any other headers it sets; never enter their values.

  5. If the browser sent a preflight, select Use preflight headers from DevTools and paste its Access-Control-Request-Headers, or leave it empty when it lists none.

  6. Select Check saved settings.

The result leads with the first thing to fix, then explains the Preflight and the Actual response: who answers the preflight, what must hold before it is answered, and the headers the project sends. It checks the saved setting, so save your changes first.

For example, a page on http://localhost:5173 that sends credentials: 'include' to a project set to Any website gets:

Blocked: credentials with any website
Your app includes credentials, but this project sends Access-Control-Allow-Origin: *. The browser requires your exact page origin and Access-Control-Allow-Credentials: true.

Choose Only these origins, add http://localhost:5173, save, and check again.

When you enter a path, the result offers a command to copy that sends the same preflight to the project’s address, so you can compare the real answer:

Terminal window
curl --globoff --silent --show-error --dump-header - --output /dev/null \
--request OPTIONS \
--header 'Origin: http://localhost:5173' \
--header 'Access-Control-Request-Method: PATCH' \
--header 'Access-Control-Request-Headers: authorization,content-type' \
-- 'https://orders-api.mockhub.dev/orders/42?view=summary'

The command is a real request, so an OPTIONS mock can answer it. For a POST, PATCH or DELETE, inspect the actual request in the browser’s network panel rather than sending it again.

MockHub shows Settings saved. The next answer carries the new headers. A browser can keep using a preflight answer it already has for up to ten minutes, so after you add your first OPTIONS mock, wait that long or open a private window.

  • A mock that sets its own Access-Control-Allow-Origin header keeps it, and MockHub adds nothing to that answer.
  • With Only these origins, a page whose origin is not listed still sends its requests, and MockHub still answers them, without the CORS headers: the browser then refuses to give the response to the page. CORS stops a page reading a response; it does not keep anyone from calling the project.
  • An origin cannot contain a wildcard, such as https://*.vercel.app. Use Any website for preview deployments, or list each one. A refused origin is named before its reason, such as localhost:5173: An origin is a scheme, a host and an optional port, such as http://localhost:5173.
  • The request log shows each answer as the mock or your real API gave it, without the CORS headers MockHub added.
  • When the project records from your real API, MockHub replaces that API’s own CORS headers with the project’s, because the browser is calling MockHub’s address, not the API’s. The recorded answer keeps the API’s headers.
  • Changing this setting needs the Admin role. Check browser access needs only the Viewer role.
  • Check browser access explains the project’s setting, not your mocks: a mock that sends its own Access-Control-Allow-Origin replaces everything it describes. To see which mock answers, use Test a request on the Mocks tab, which this check also links to.

Report a problem with this page