chenhg5/cc-connect · error

invalid json response: %w

Error message

invalid json response: %w

What it means

verifyWeixinToken decodes the 200 response into a generic map[string]any to confirm it is well-formed JSON. If json.Unmarshal fails, verification fails with this error. The endpoint returned 200 but the body was not parseable JSON, so the token's validity cannot be confirmed.

Source

Thrown at cmd/cc-connect/weixin.go:539

	}
	defer resp.Body.Close()
	raw, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
	if err != nil {
		return err
	}
	if debug {
		snippet := string(raw)
		if len(snippet) > 300 {
			snippet = snippet[:300]
		}
		fmt.Fprintf(os.Stderr, "[debug] verify getUpdates -> %d %s\n", resp.StatusCode, strings.TrimSpace(snippet))
	}
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("http %d: %s", resp.StatusCode, weixinTruncateBody(raw, 256))
	}
	var parsed map[string]any
	if err := json.Unmarshal(raw, &parsed); err != nil {
		return fmt.Errorf("invalid json response: %w", err)
	}
	return nil
}

func randomWeixinUIN() string {
	var b [4]byte
	if _, err := rand.Read(b[:]); err != nil {
		return base64.StdEncoding.EncodeToString([]byte("0000"))
	}
	u := uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3])
	return base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%d", u)))
}

func printWeixinUsage() {
	fmt.Println(`Usage: cc-connect weixin <command> [options]

Commands:
  setup   QR login when no --token; with --token => bind existing ilink bot token

View on GitHub (pinned to 4000b2338a)

Solutions

  1. Dump the raw response body to see what was returned
  2. Verify the api_base scheme/host/port actually hosts the WeChat bot API
  3. Disable or bypass intercepting proxies/VPN for this request
  4. Retry — intermittent HTML error pages from a load balancer can produce this
Defensive patterns

Strategy: type-guard

Validate before calling

if !json.Valid(raw) {
    return fmt.Errorf("verify: non-JSON 200 response: %.100s", raw)
}

Type guard

func isJSONObject(b []byte) bool {
    var v map[string]any
    return json.Unmarshal(b, &v) == nil && v != nil
}

Try / catch

if err := verifyWeixinToken(ctx, apiBase, token, routeTag, debug); err != nil {
    if strings.Contains(err.Error(), "invalid json response") {
        return fmt.Errorf("weixin: api_base %s did not return JSON — check the URL", apiBase)
    }
    return err
}

Prevention

When it happens

Trigger: Token verification GET returns HTTP 200 with an HTML page, empty body, or malformed JSON instead of the expected JSON object.

Common situations: Intercepting proxy or wrong port returning a 200 HTML page; api_base misconfiguration pointing at a non-API service; middleboxes that fake success responses.

Understand the failure class

Background: "Invalid JSON response" and "Failed to parse response" errors: when an API answers 200 but the body isn't the JSON your library expected — this error's family across 28 libraries.

Related errors


AI-assisted analysis of chenhg5/cc-connect@4000b2338a (2026-09-06). Data as JSON: /api/errors/d95adfadb4c5495a. Report an issue: GitHub.