Skip to content

ctx.net stable

Server-side HTTP client — the only sanctioned way to make outbound network requests from an extension. Requests run in the Rust backend via reqwest, so they bypass the browser's CORS policy and can read response headers that the browser would otherwise hide from cross-origin requests.

ts
ctx.net: NetworkService

Typical use-cases:

  • Checking X-Frame-Options / CSP frame-ancestors before embedding a URL in an <iframe> (used by the Local Web Viewer extension).
  • Probing a localhost dev server that has no CORS headers.
  • Fetching data from an external API that doesn't grant cross-origin access.

Methods

ctx.net.fetch(url, options?)

Make an HTTP request server-side, bypassing CORS. Returns the full response: status, headers, body, and the final URL after any redirects.

ts
fetch(url: string, options?: NetworkRequestOptions): Promise<NetworkResponse>

Throws a string error message if the request fails (network error, DNS failure, TLS error, timeout, etc.).

ts
const { status, headers, body, finalUrl } = await ctx.net.fetch(
  "https://api.example.com/data",
);

// POST with a JSON body
const res = await ctx.net.fetch("https://api.example.com/items", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ name: "example" }),
  timeoutMs: 5000,
});

ctx.net.fetchHeaders(url, options?)

Send a HEAD request and return only the response headers — no body is downloaded. More efficient than fetch when you only need to inspect headers.

ts
fetchHeaders(
  url: string,
  options?: Pick<NetworkRequestOptions, "followRedirects" | "timeoutMs">,
): Promise<Record<string, string>>

Header names are lowercased; multi-value headers are joined with ", ".

Throws a string error message if the request fails.

ts
// Check whether a URL can be embedded in an iframe
const headers = await ctx.net.fetchHeaders("http://localhost:5173", {
  timeoutMs: 8000,
});

const xfo = headers["x-frame-options"]?.toLowerCase();
const blocked = xfo === "deny" || xfo === "sameorigin";

Options

NetworkRequestOptions

FieldTypeDefaultDescription
method"GET" | "HEAD" | "POST" | "PUT" | ..."GET"HTTP method. Ignored by fetchHeaders (always HEAD).
headersRecord<string, string>Request headers to send.
bodystringRequest body. Only meaningful for methods that carry a body.
followRedirectsbooleantrueFollow HTTP redirects.
timeoutMsnumber~30 000 msRequest timeout. Omit to use the platform default.

Response shape

NetworkResponse

FieldTypeDescription
statusnumberHTTP status code.
headersRecord<string, string>Lowercased headers; multi-values joined with ", ".
bodystringResponse body decoded as UTF-8 text.
finalUrlstringFinal URL after redirects (equals url if none).

See also