Tencent/WeKnora · error

http decode read response: %w

Error message

http decode read response: %w

What it means

This error indicates the JSON response from the HTTP document-parse service could not be decoded into the expected httpReadResponse struct. It is thrown after a successful 200 response when the body does not match the expected schema. The underlying decode error is wrapped (%w) so json syntax/type errors are preserved.

Source

Thrown at internal/infrastructure/docparser/http_parser.go:242

	httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, base+PathRead, bytes.NewReader(jsonBody))
	if err != nil {
		return nil, fmt.Errorf("http new request: %w", err)
	}
	httpReq.Header.Set("Content-Type", "application/json")
	httpReq.ContentLength = int64(len(jsonBody))

	resp, err := p.client.Do(httpReq)
	if err != nil {
		return nil, fmt.Errorf("http read failed: %w", err)
	}
	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		bodyBytes, _ := io.ReadAll(resp.Body)
		return nil, fmt.Errorf("http read status %d: %s", resp.StatusCode, string(bodyBytes))
	}
	var out httpReadResponse
	if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
		return nil, fmt.Errorf("http decode read response: %w", err)
	}
	return fromHTTPReadResponse(&out), nil
}

View on GitHub (pinned to 988cbb0330)

Solutions

  1. Unwrap the error to see the exact json decode failure and offset
  2. Check that the parser service version matches the client's expected response schema
  3. Verify the configured URL points at the parser endpoint, not an HTML page or proxy
  4. Log the raw response body on decode failure to diagnose schema drift

Example fix

// before
var out httpReadResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
    return nil, fmt.Errorf("http decode read response: %w", err)
}
// after
raw, _ := io.ReadAll(resp.Body)
var out httpReadResponse
if err := json.Unmarshal(raw, &out); err != nil {
    return nil, fmt.Errorf("http decode read response: %w (body=%.200q)", err, raw)
}
Defensive patterns

Strategy: try-catch

Validate before calling

// verify endpoint returns JSON before decoding
resp, err := http.Get(parserURL)
if err == nil && !strings.HasPrefix(resp.Header.Get("Content-Type"), "application/json") {
    return fmt.Errorf("parser endpoint returned %s, expected JSON", resp.Header.Get("Content-Type"))
}

Try / catch

out, err := parser.Read(ctx, doc)
if err != nil {
    if strings.Contains(err.Error(), "http decode read response") {
        // schema mismatch or HTML error page: log raw body, check service version
    }
    return err
}

Prevention

When it happens

Trigger: Calling Read on the HTTP doc parser when the service returns 200 but the body is not valid JSON or lacks the expected fields (json.UnmarshalTypeError / SyntaxError).

Common situations: Service version mismatch where response schema changed; proxy/captive portal returning an HTML error page with 200; truncated response; wrong endpoint returning non-JSON content.

Related errors


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