Tencent/WeKnora · error

create request: %w

Error message

create request: %w

What it means

http.NewRequestWithContext failed while building the POST to endpoint + "/file_parse". This error means the request could not be constructed — almost always an invalid URL (unparseable/empty endpoint, bad characters, unsupported scheme) rather than a network problem.

Source

Thrown at internal/infrastructure/docparser/mineru_converter.go:238

	for k, v := range fields {
		_ = writer.WriteField(k, v)
	}

	uploadFileName := minerUUploadFileName(fileName, fileType)

	// File part
	part, err := writer.CreateFormFile("files", uploadFileName)
	if err != nil {
		return "", nil, fmt.Errorf("create form file: %w", err)
	}
	if _, err := part.Write(content); err != nil {
		return "", nil, fmt.Errorf("write file content: %w", err)
	}
	writer.Close()

	httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpoint+"/file_parse", &body)
	if err != nil {
		return "", nil, fmt.Errorf("create request: %w", err)
	}
	httpReq.Header.Set("Content-Type", writer.FormDataContentType())

	client := utils.NewSSRFSafeHTTPClient(utils.SSRFSafeHTTPClientConfig{
		Timeout:      mineruTimeout,
		MaxRedirects: 5,
	})
	resp, err := client.Do(httpReq)
	if err != nil {
		return "", nil, fmt.Errorf("HTTP request: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		respBody, _ := io.ReadAll(resp.Body)
		return "", nil, fmt.Errorf("MinerU API status %d: %s", resp.StatusCode, string(respBody))
	}

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Log/inspect c.endpoint and validate it with url.Parse before constructing requests.
  2. Fix the configured base URL: it must be an absolute http(s) URL, e.g. http://mineru:8000 (no trailing whitespace/newline).
  3. Add a startup config check that fails fast if the endpoint doesn't parse.
  4. Trim whitespace when loading the endpoint from env/config files.

Example fix

// before
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, c.endpoint+"/file_parse", &body)
if err != nil {
    return "", nil, fmt.Errorf("create request: %w", err)
}
// after
baseURL := strings.TrimSpace(c.endpoint)
if u, perr := url.Parse(baseURL + "/file_parse"); perr != nil || u.Scheme == "" || u.Host == "" {
    return "", nil, fmt.Errorf("invalid mineru endpoint %q: %w", baseURL, perr)
}
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, baseURL+"/file_parse", &body)
Defensive patterns

Strategy: validation

Validate before calling

u, err := url.Parse(strings.TrimSpace(endpoint) + "/file_parse")
if err != nil || u.Scheme == "" || u.Host == "" {
    return fmt.Errorf("invalid mineru endpoint %q", endpoint)
}

Type guard

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

Try / catch

if err != nil && strings.Contains(err.Error(), "create request") {
    // configuration bug, not transient — fail fast, surface config error
    return fmt.Errorf("misconfigured mineru endpoint: %w", err)
}

Prevention

When it happens

Trigger: c.endpoint is empty, malformed (missing scheme, spaces, control chars), or c.endpoint + "/file_parse" doesn't parse as a valid URL.

Common situations: Misconfigured MinerU endpoint in config/env (e.g. missing http://, trailing newline or whitespace from an env var, wrong port); endpoint set to a unix-socket or relative path string.

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


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