sindresorhus/got · error · Error

`url` protocol must be followed by `//`

Error message

`url` protocol must be followed by `//`

What it means

Thrown by the `url` setter when the URL string looks like an HTTP(S) protocol but is not followed by `//` (matched by `/^https?:(?!\/\/)/`). Got requires well-formed `http://`/`https://` URLs; a value like `http:example.com` is ambiguous and would be mis-parsed, so it is rejected up front.

Source

Thrown at source/core/options.ts:2116

		if (value === undefined) {
			this.#internals.url = undefined;
			trackStateMutation(this.#trackedStateMutations, 'url');
			return;
		}

		if (is.string(value) && value.startsWith('/')) {
			throw new Error('`url` must not start with a slash');
		}

		const valueString = value.toString();

		if (
			is.string(value)
			&& !this.prefixUrl
			&& hasHttpProtocolWithoutSlashes(valueString)
		) {
			throw new Error('`url` protocol must be followed by `//`');
		}

		// Detect if URL is already absolute.
		const isAbsolute = isAbsoluteUrl(value);
		assertRelativeUrlIfNeeded(this, value);

		// Only concatenate prefixUrl if the URL is relative
		const urlString = isAbsolute ? valueString : `${this.prefixUrl as string}${valueString}`;
		const url = new URL(urlString);
		this.#internals.url = url;
		trackStateMutation(this.#trackedStateMutations, 'url');

		if (usesUnixSocket(url) && !this.#internals.enableUnixSockets) {
			throw new Error('Using UNIX domain sockets but option `enableUnixSockets` is not enabled');
		}

		if (url.protocol === 'unix:') {
			url.href = `http://unix${url.pathname}${url.search}`;

View on GitHub (pinned to e3924aa1e5)

Solutions

  1. Use a fully-formed URL: `got('http://example.com')`.
  2. If building programmatically, ensure the separator is `://`.
  3. Set a `prefixUrl` if you intend the value to be a relative path.

Example fix

// before
await got('http:example.com/api');
// after
await got('http://example.com/api');
Defensive patterns

Strategy: validation

Validate before calling

function assertWellFormedUrl(url) {
  if (typeof url === 'string' && /^https?:([^/]|$)/i.test(url) && !/^https?:\/\//i.test(url)) {
    throw new Error(`URL protocol must be followed by '//': ${url}`);
  }
}

Type guard

function isWellFormedAbsoluteUrl(v: unknown): boolean {
  return typeof v === 'string' && /^https?:\/\//i.test(v);
}

Prevention

When it happens

Trigger: Calling `got('http:example.com')`, `got('https:/example.com')` (single slash), or any string matching `http:` or `https:` not immediately followed by `//`. Only fires when there is no `prefixUrl` (with a prefixUrl the value is treated as relative and concatenated).

Common situations: Constructing URLs by string concatenation that drops a slash (`'http:' + host`); copy-paste artifacts; config values that were trimmed or lowercased incorrectly.

Related errors


AI-assisted analysis of sindresorhus/got@e3924aa1e5 (2026-08-03). Data as JSON: /data/errors/6ad997c45bf292af.json. Report an issue: GitHub.