vxcontrol/pentagi · error · Fatal

failed to create request: %w

Error message

failed to create request: %w

What it means

http.NewRequestWithContext fails to parse the method/URL/body for the POST to sploitusAPIURL; the searcher converts it to a Fatal error. Since sploitusAPIURL is a package constant, this fires only if the constant is a malformed URL or the marshaled body reader is nil/invalid.

Source

Thrown at backend/pkg/tools/searchers/sploitus.go:132

		Title:  false, // search only for titles
		Offset: 0,
	}

	bodyBytes, err := json.Marshal(reqBody)
	if err != nil {
		return "", Fatal(fmt.Errorf("failed to marshal request body: %w", err))
	}

	client, err := system.GetHTTPClient(s.cfg)
	if err != nil {
		return "", Fatal(fmt.Errorf("failed to create http client: %w", err))
	}

	client.Timeout = sploitusRequestTimeout

	req, err := http.NewRequestWithContext(ctx, http.MethodPost, sploitusAPIURL, bytes.NewReader(bodyBytes))
	if err != nil {
		return "", Fatal(fmt.Errorf("failed to create request: %w", err))
	}

	// Build referer with query to mimic browser behavior
	referer := fmt.Sprintf("https://sploitus.com/?query=%s", url.QueryEscape(query))

	// Mimic Chrome browser headers to bypass Cloudflare protection
	req.Header.Set("Accept", "application/json")
	req.Header.Set("Accept-Language", "en-US,en;q=0.9")
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Origin", "https://sploitus.com")
	req.Header.Set("Referer", referer)
	req.Header.Set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36")
	req.Header.Set("sec-ch-ua", `"Not:A-Brand";v="99", "Google Chrome";v="145", "Chromium";v="145"`)
	req.Header.Set("sec-ch-ua-mobile", "?0")
	req.Header.Set("sec-ch-ua-platform", `"macOS"`)
	req.Header.Set("sec-fetch-dest", "empty")
	req.Header.Set("sec-fetch-mode", "cors")
	req.Header.Set("sec-fetch-site", "same-origin")

View on GitHub (pinned to ea665308ba)

Solutions

  1. Read the wrapped error (it names the parse failure) and correct the sploitusAPIURL constant
  2. Verify the URL parses: scheme must be http/https, no whitespace or control chars
  3. If the URL is configurable, validate it at startup instead of inside the request path

Example fix

// before
const sploitusAPIURL = "htp://sploitus.com/search"
// after
const sploitusAPIURL = "https://sploitus.com/search"
Defensive patterns

Strategy: validation

Validate before calling

if u, err := url.Parse(sploitusAPIURL); err != nil || (u.Scheme != "http" && u.Scheme != "https") {
    log.Fatalf("invalid sploitus API url: %v", err)
}

Try / catch

req, err := http.NewRequestWithContext(ctx, http.MethodPost, sploitusAPIURL, bytes.NewReader(bodyBytes))
if err != nil {
    return "", Fatal(fmt.Errorf("failed to create request: %w", err))
}

Prevention

When it happens

Trigger: Handle() calls http.NewRequestWithContext(ctx, http.MethodPost, sploitusAPIURL, bytes.NewReader(bodyBytes)); error occurs when sploitusAPIURL fails url.Parse (bad scheme, control characters) — essentially only after an edit to the constant.

Common situations: A developer fat-fingers the sploitusAPIURL constant (e.g. typo like "htp://" or trailing spaces), or refactors to build the URL dynamically from config with an unparsable value.

Understand the failure class

Background: "Invalid URL" errors: why new URL(), URI.parse, and reqwest::Url reject your string — missing scheme, whitespace, and bad path format — this error's family across 39 libraries.

Related errors


AI-assisted analysis of vxcontrol/pentagi@ea665308ba (2026-09-01). Data as JSON: /api/errors/221f730173c03053. Report an issue: GitHub.