Skip to content

A browser cannot call my mock

A request that works from curl or a test runner can fail in a browser, because the browser adds rules of its own. The page’s console then shows an error such as one of these; Chrome writes XMLHttpRequest instead of fetch for older request code:

Access to fetch at 'https://orders-api.mockhub.dev/orders' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://orders-api.mockhub.dev/orders. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing). Status code: 200.
Mixed Content: The page at 'https://app.example.com/' was loaded over HTTPS, but requested an insecure resource 'http://orders.mocks.example.com/orders'. This request has been blocked; the content must be served over HTTPS.

These messages come from the browser, not from MockHub, and their wording varies between browsers and versions.

On the project’s Settings tab, select Check browser access, describe the failing request and select Check saved settings. The result names the first cause below that applies to the saved setting, and gives a curl command for the deployed address.

Then compare it with what the browser received. In the network panel, open the failing request and read its response headers: Access-Control-Allow-Origin, and Access-Control-Allow-Credentials when the page sends credentials. If the browser sent a preflight, read the OPTIONS entry too; a missing one can mean the browser reused a cached answer. A preflight MockHub answers itself is not stored in the request logs, so a missing log row is expected, and a request log shows the response before MockHub adds its CORS headers.

A project answers every page unless an Admin has changed its Browser access (CORS) setting. With Off, MockHub adds no CORS headers and answers no preflight, so every mock has to send its own.

  1. Open the project’s Settings tab, and go to Responses and logging.
  2. Under Browser access (CORS), select Any website, or Only these origins and list the page’s origin.
  3. Select Save settings.

See Let web pages call a project.

Your page’s origin is not on the project’s list

Section titled “Your page’s origin is not on the project’s list”

With Only these origins, MockHub answers a page whose origin is not listed without any CORS headers, so the console shows the same message as for a project with CORS off. The origin has to match exactly. Compare the Origin request header in the browser’s network panel with the list, character by character; the usual differences are:

  • the port: http://localhost:3000 is not http://localhost:5173;
  • the scheme: http://localhost:5173 is not https://localhost:5173;
  • the name: http://localhost:5173 is not http://127.0.0.1:5173.

Add the origin exactly as the network panel shows it, under Origins allowed to call this project.

The page sends cookies to a project that answers any website

Section titled “The page sends cookies to a project that answers any website”

A request that sends cookies or other credentials, such as fetch with credentials: 'include', cannot use an answer for any website:

Access to fetch at 'https://orders-api.mockhub.dev/orders' from origin 'http://localhost:3000' has been blocked by CORS policy: The value of the 'Access-Control-Allow-Origin' header in the response must not be the wildcard '*' when the request's credentials mode is 'include'.

Select Only these origins and list the page’s origin: MockHub then answers that origin with Access-Control-Allow-Credentials: true.

A mock whose response sets Access-Control-Allow-Origin keeps it, and MockHub adds nothing to that answer. If the value names another origin, the browser refuses the response. Remove the header from the mock to let the project’s setting answer, or correct its value.

While a project has an active mock whose HTTP method is OPTIONS, the browser’s preflights go to its mocks, and MockHub answers only the ones no mock matched. An OPTIONS mock that sets its own Access-Control-Allow-Origin must also send Access-Control-Allow-Methods and Access-Control-Allow-Headers naming what the page sends:

Access to fetch at 'https://orders-api.mockhub.dev/orders' from origin 'http://localhost:3000' has been blocked by CORS policy: Request header field authorization is not allowed by Access-Control-Allow-Headers in preflight response.

For example, a page sends PATCH with Authorization and a JSON body, and the project has an OPTIONS mock that answers 204 with Access-Control-Allow-Origin: * and Access-Control-Allow-Headers: content-type. Check browser access says the preflight goes to your mocks first. The copied command shows the mock’s answer:

HTTP/2 204
access-control-allow-origin: *
access-control-allow-headers: content-type

authorization is missing, and * never covers it. A mock that answers a preflight also needs a successful status: an error status fails the preflight whatever its headers say.

Add the missing names to the mock’s headers, or deactivate the OPTIONS mock to let MockHub answer every preflight. A browser can keep a preflight answer for up to ten minutes, so retry after that, or in a private window.

A page loaded over HTTPS cannot call an http:// address, whatever the mock’s headers say: the browser blocks the request before it is sent, so nothing reaches MockHub or its request logs.

Call the project’s https:// address, as listed under the project’s name. If the project’s custom domain serves HTTP only, an Admin can change it to HTTP and HTTPS under Protocols on the domain’s row; see Choose HTTPS, HTTP or both for a domain.

On a domain set to HTTPS only, an http:// request is redirected to the same address over HTTPS. The redirect carries no CORS headers, so a page on another origin cannot follow it, and a browser never follows a redirect for a preflight request:

Access to fetch at 'http://orders.mocks.example.com/orders' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: Redirect is not allowed for a preflight request.

Call the https:// address instead.

Some top-level domains, such as .dev and .app, are HTTPS-only in every major browser: it turns an http:// address under them into https:// before sending anything. On a custom domain under such a top-level domain that is set to HTTP only, the browser’s HTTPS connection then fails, or gets this answer:

{
"type": "https://tools.ietf.org/html/rfc7807",
"title": "Misdirected Request",
"status": 421,
"detail": "This domain serves HTTP only."
}

The domain’s row warns about this, with a message such as Browsers with the .dev preload require HTTPS. HTTP is available to clients that allow cleartext. Set the domain to HTTP and HTTPS, and call the https:// address. See Hostnames, domains and protocols.

Report a problem with the full console message, the page’s origin and the address it called.

Report a problem with this page