Script recipes
Each recipe is a whole script. Save one as a .js file and Import… it in the Scripts window, and its header sets its name, its URL and when it runs. Or paste it into the editor: the header is only comments there. Change api.example.com to the app’s own host.
For every helper they use, see the script reference.
Tokens
Section titled “Tokens”Keep the token a login returns
Section titled “Keep the token a login returns”// ==zhttp==// @name Keep the login token// @url https://api.example.com/v1/auth/login*// @method POST// @run response// @enabled true// ==/zhttp==function onResponse(context, url, request, response) { const token = json.get(response.body, "data.accessToken"); if (token) { shared.set("token", token); console.log("kept a new token"); } return response;}The token shows in the Variables tab, and lasts until the session is closed.
Send the kept token
Section titled “Send the kept token”// ==zhttp==// @name Send the kept token// @url https://api.example.com/*// @run request// @enabled true// ==/zhttp==function onRequest(context, url, request) { if (shared.has("token")) { request.headers["Authorization"] = "Bearer " + shared.get("token"); } return request;}With the one above, every request goes as whoever logged in last, even from another device on the proxy. Or paste a token into the Variables tab as token.
See whose token it is
Section titled “See whose token it is”// ==zhttp==// @name Whose token// @url https://api.example.com/*// @run request// @enabled true// ==/zhttp==function onRequest(context, url, request) { const auth = request.headers["Authorization"]; if (auth && auth.startsWith("Bearer ey")) { const claims = jwt.decode(auth.slice(7)).payload; console.log("user", claims.sub, "expires", time.iso(claims.exp * 1000)); } return request;}Sign a changed request again
Section titled “Sign a changed request again”An API that signs each request turns away one a script changed. This one signs it again, here as an HMAC of the time, the method, the path and the body:
// ==zhttp==// @name Sign again// @url https://api.example.com/*// @method POST// @run request// @enabled true// ==/zhttp==function onRequest(context, url, request) { const body = JSON.stringify(request.body); const timestamp = String(time.unix()); request.body = body; request.headers["X-Timestamp"] = timestamp; request.headers["X-Signature"] = hmac("sha256", shared.get("signingKey"), timestamp + request.method + request.path + body, "base64"); return request;}Put the key in the Variables tab as signingKey, and keep this script last, so that it signs what the scripts above it made. The body is sent as the text that was signed.
What the app gets
Section titled “What the app gets”Answer an endpoint yourself
Section titled “Answer an endpoint yourself”// ==zhttp==// @name Fake profile// @url https://api.example.com/v2/me*// @method GET// @run response// @enabled true// ==/zhttp==function onResponse(context, url, request, response) { response.statusCode = 200; response.headers["Content-Type"] = "application/json"; response.body = { id: 42, name: "Test User", plan: "pro", trialEndsAt: null, }; return response;}The request still goes to the server. The app gets this in place of what it answered.
Turn on every feature flag
Section titled “Turn on every feature flag”// ==zhttp==// @name All flags on// @url https://api.example.com/v1/config*// @run response// @enabled true// ==/zhttp==function onResponse(context, url, request, response) { const flags = json.get(response.body, "data.flags") ?? {}; for (const name of Object.keys(flags)) { flags[name] = true; } return response;}Show the app an empty list
Section titled “Show the app an empty list”// ==zhttp==// @name No orders// @url https://api.example.com/v1/orders*// @method GET// @run response// @enabled true// ==/zhttp==function onResponse(context, url, request, response) { json.set(response.body, "data.items", []); json.set(response.body, "data.total", 0); return response;}Point the app at staging
Section titled “Point the app at staging”When the app reads its servers from a config it downloads:
// ==zhttp==// @name Use staging// @url https://api.example.com/v1/config*// @run response// @enabled true// ==/zhttp==function onResponse(context, url, request, response) { replaceInBody(response, /https:\/\/api\.example\.com/g, "https://staging-api.example.com"); return response;}Fail one call in ten
Section titled “Fail one call in ten”// ==zhttp==// @name Flaky// @url https://api.example.com/*// @run response// @enabled true// ==/zhttp==function onResponse(context, url, request, response) { if (random.int(1, 10) === 1) { response.statusCode = 503; response.headers["Retry-After"] = "5"; response.headers["Content-Type"] = "application/json"; response.body = { error: "unavailable" }; } return response;}Slow an endpoint down
Section titled “Slow an endpoint down”// ==zhttp==// @name Slow search// @url https://api.example.com/v2/search*// @run response// @enabled true// ==/zhttp==function onResponse(context, url, request, response) { sleep(3000); return response;}A run can sleep for up to 30 seconds in all.
What the server gets
Section titled “What the server gets”Strip tracking parameters
Section titled “Strip tracking parameters”// ==zhttp==// @name No tracking parameters// @url *// @run request// @enabled true// ==/zhttp==function onRequest(context, url, request) { for (const name of Object.keys(request.queries)) { if (name.startsWith("utm_") || name === "fbclid" || name === "gclid") { delete request.queries[name]; } } return request;}Drop tracking cookies
Section titled “Drop tracking cookies”// ==zhttp==// @name No tracking cookies// @url *// @run request// @enabled true// ==/zhttp==function onRequest(context, url, request) { for (const name of Object.keys(request.cookies)) { if (/^(_ga|_gid|_fbp|_gcl)/.test(name)) { delete request.cookies[name]; } } return request;}Change a GraphQL query’s variables
Section titled “Change a GraphQL query’s variables”// ==zhttp==// @name Bigger search pages// @url https://api.example.com/graphql*// @method POST// @run request// @enabled true// ==/zhttp==function onRequest(context, url, request) { const operations = Array.isArray(request.body) ? request.body : [request.body]; for (const operation of operations) { if (operation.operationName === "SearchProducts") { operation.variables.first = 100; operation.variables.filter = { inStock: true }; } } return request;}An array is a batch of operations sent together.
Always get the whole response
Section titled “Always get the whole response”A server answers 304 Not Modified, with no body, when the app already has what it would send. Without the headers that ask for that, it sends the body every time:
// ==zhttp==// @name No 304s// @url https://api.example.com/*// @run request// @enabled true// ==/zhttp==function onRequest(context, url, request) { delete request.headers["If-None-Match"]; delete request.headers["If-Modified-Since"]; return request;}Different headers for each host
Section titled “Different headers for each host”// ==zhttp==// @name Headers by host// @url https://*.example.com/*// @run request// @enabled true// ==/zhttp==function onRequest(context, url, request) { if (matches(url, "https://api.example.com/*")) { request.headers["X-Api-Version"] = "2"; } else if (matches(url, "https://images.example.com/*")) { request.headers["Accept"] = "image/png"; } request.headers["X-Request-Id"] = uuid(); return request;}Looking
Section titled “Looking”Log the calls that fail
Section titled “Log the calls that fail”// ==zhttp==// @name Failed calls// @url https://api.example.com/*// @run response// @enabled true// ==/zhttp==function onResponse(context, url, request, response) { if (response.statusCode >= 400) { const failed = shared.increment("failed"); console.warn(failed, response.statusCode, request.method, request.path, json.get(response.body, "error.message")); } return response;}Each line in the console links to its request. The count shows in the Variables tab as failed.