router-for-me/CLIProxyAPI · error

invalid request body

Error message

invalid request body

What it means

When the delete-request body starts with `[`, it is parsed as a JSON array of file names. If json.Unmarshal fails, the handler returns `invalid request body` (array branch, auth_files_crud.go:306). The body was read fine but is not a valid JSON array of strings — e.g. trailing commas, quoted-but-object content, or array elements of the wrong type.

Source

Thrown at internal/api/handlers/management/auth_files_crud.go:306

	}

	body, err := io.ReadAll(c.Request.Body)
	if err != nil {
		return nil, fmt.Errorf("failed to read body")
	}
	body = bytes.TrimSpace(body)
	if len(body) == 0 {
		return nil, nil
	}

	var objectBody struct {
		Name  string   `json:"name"`
		Names []string `json:"names"`
	}
	if body[0] == '[' {
		var arrayBody []string
		if err := json.Unmarshal(body, &arrayBody); err != nil {
			return nil, fmt.Errorf("invalid request body")
		}
		return uniqueAuthFileNames(arrayBody), nil
	}
	if err := json.Unmarshal(body, &objectBody); err != nil {
		return nil, fmt.Errorf("invalid request body")
	}

	out := make([]string, 0, len(objectBody.Names)+1)
	if strings.TrimSpace(objectBody.Name) != "" {
		out = append(out, objectBody.Name)
	}
	out = append(out, objectBody.Names...)
	return uniqueAuthFileNames(out), nil
}

func uniqueAuthFileNames(names []string) []string {
	if len(names) == 0 {
		return nil

View on GitHub (pinned to 78f0c4079e)

Solutions

  1. Validate the array JSON with jq before sending: jq -c '.[0]' <<< body should succeed.
  2. Send names via query params (?name=a&name=b) to skip body parsing entirely.
  3. Use an object body {"names":[...]} which is also accepted.

Example fix

# before
$ curl -X DELETE .../auth-files -d '["a.json",]'

# after
$ curl -X DELETE .../auth-files -d '["a.json"]'
# or
$ curl -X DELETE '.../auth-files?name=a.json'
Defensive patterns

Strategy: validation

Validate before calling

// Validate the array body client-side before sending
func validNamesArray(body []byte) bool {
    var names []string
    return json.Unmarshal(body, &names) == nil
}

Type guard

func isInvalidRequestBody(err error) bool {
    return err != nil && strings.Contains(err.Error(), "invalid request body")
}

Try / catch

if isInvalidRequestBody(err) {
    names := extractNames()
    err = deleteViaQueryParams(names) // bypass body parsing entirely
}

Prevention

When it happens

Trigger: DELETE body like `["a.json",]` (trailing comma), `[1,2]` (non-string elements), `["a.json"` (truncated JSON), or a body beginning with whitespace-stripped `[` that is actually malformed text.

Common situations: Hand-built curl bodies with typos; scripts joining names with trailing separators; JSON5-style input from LLM-generated commands.

Related errors


AI-assisted analysis of router-for-me/CLIProxyAPI@78f0c4079e (2026-08-15). Data as JSON: /api/errors/3b1c58f14c1210ed. Report an issue: GitHub.