Custom Headers
Inject CORS and custom response headers into tunnel traffic
Overview
The --header flag adds HTTP headers to the responses that pass through your tunnel. It is most useful for CORS during development.
CORS example
zgrok http 3000 --header "Access-Control-Allow-Origin: *"Check that it arrives:
$ curl -sI https://a7f9x2--acme-shop.zgrok.io/ | grep -i access-controlAccess-Control-Allow-Origin: *
Multiple headers
Use --header multiple times to inject several headers:
zgrok http 3000 \
--header "Access-Control-Allow-Origin: *" \
--header "Access-Control-Allow-Methods: GET, POST, PUT, DELETE" \
--header "Access-Control-Allow-Headers: Content-Type, Authorization"Common use cases
CORS for frontend development
When your frontend (on localhost:5173) calls your tunnel URL:
zgrok http 8080 --header "Access-Control-Allow-Origin: http://localhost:5173"This is enough for a simple request, such as a plain GET. Two things it cannot do for you:
- Preflight. Before a request with a JSON body, an
Authorizationheader or a method such asPUT, the browser sends anOPTIONSrequest first. zgrok adds your headers to that answer too, but the status is your app's. If your app answersOPTIONSwith a404or405, the browser still blocks the request, so the route has to answer it with a2xx. - Cookies. A request sent with credentials needs a named origin, not
*, and also--header "Access-Control-Allow-Credentials: true".
Custom security headers
zgrok http 3000 \
--header "X-Frame-Options: DENY" \
--header "X-Content-Type-Options: nosniff"Cache control
zgrok http 3000 --header "Cache-Control: no-store"How it works
The zgrok server intercepts the HTTP response before sending it to the client and appends your custom headers. They are also on the page zgrok shows when your local server is not running. They are not added to a WebSocket upgrade, or to a response zgrok refuses before it reaches your machine, such as a 403 from --allow-ip.
A header your app already sends is not replaced: both are sent. For CORS that breaks the request, because a browser refuses a response with two Access-Control-Allow-Origin values. If your app already sets CORS headers, change them there instead of using --header.
Refused headers
A header zgrok cannot accept stops the tunnel before it starts, so a protection you asked for is never silently missing:
$ zgrok http 3000 --header "no-colon"Error: invalid --header "no-colon": expected "Name: Value"$ zgrok http 3000 --header "Set-Cookie: a=b"Error: invalid --header "Set-Cookie: a=b": set-cookie may not be set by a tunnel
Set-Cookie, Set-Cookie2 and Clear-Site-Data are refused; your own app can still send them. A name with a space or a value with a line break is refused too.
See also
- CLI Reference: all flags
- Troubleshooting: CORS and other common errors