Tencent/WeKnora · error

failed to create request: %w

Error message

failed to create request: %w

What it means

http.NewRequestWithContext failed while building the Bing GET request in buildParams, and the error is wrapped with this message. Given the URL is already encoded at this point, failure almost always means the resulting URL failed http-request parsing.

Source

Thrown at internal/infrastructure/web_search/bing.go:194

			Items []struct {
				AnswerType string `json:"answerType"`
				Value      struct {
					ID string `json:"id"`
				} `json:"value"`
			} `json:"items"`
		} `json:"sidebar"`
	} `json:"rankingResponse"`
}

func (p *BingProvider) buildParams(ctx context.Context, query string, maxResults int, includeDate bool) (*http.Request, error) {
	params := url.Values{}
	params.Set("q", query)
	params.Set("count", strconv.Itoa(maxResults))

	queryURL := fmt.Sprintf("%s?%s", p.baseURL, params.Encode())
	req, err := http.NewRequestWithContext(ctx, "GET", queryURL, nil)
	if err != nil {
		return nil, fmt.Errorf("failed to create request: %w", err)
	}
	req.Header.Set("User-Agent", defaultUserAgentHeader)
	req.Header.Set("Ocp-Apim-Subscription-Key", p.apiKey)
	return req, nil
}

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Inspect the wrapped error text; it names the offending URL component.
  2. Verify the configured Bing endpoint is a full, valid absolute URL (e.g. https://api.bing.microsoft.com/v7.0/search).
  3. Trim whitespace/quotes when loading the baseURL from env or config.
  4. Pass a live, non-canceled context to Search (a canceled ctx can make the request construction fail).

Example fix

// before
baseURL: os.Getenv("BING_SEARCH_URL")
// after
baseURL := strings.TrimSpace(os.Getenv("BING_SEARCH_URL"))
if u, err := url.Parse(baseURL); err != nil || u.Scheme == "" || u.Host == "" {
    return errors.New("BING_SEARCH_URL must be an absolute https URL")
}
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(cfg.BingBaseURL)
if err != nil || u.Scheme != "https" || u.Host == "" {
    return errors.New("bing baseURL must be an absolute https URL")
}

Type guard

func isValidURL(s string) bool {
    u, err := url.Parse(s)
    return err == nil && (u.Scheme == "http" || u.Scheme == "https") && u.Host != ""
}

Try / catch

results, err := provider.Search(ctx, query, 10, false)
if err != nil && strings.Contains(err.Error(), "failed to create request") {
    // configuration bug, not transient — surface as startup/config error
    return fmt.Errorf("bing provider misconfigured: %w", err)
}

Prevention

When it happens

Trigger: baseURL is empty, malformed, or contains characters that make the assembled queryURL unparseable (e.g. spaces, invalid scheme) during a Search call.

Common situations: Misconfigured config file where the Bing endpoint is blank or contains whitespace; env var not expanded, leaving something like "${BING_URL}" or "http:// example.com" as baseURL.

Related errors


AI-assisted analysis of Tencent/WeKnora@988cbb0330 (2026-09-02). Data as JSON: /api/errors/64746eaee583f734. Report an issue: GitHub.