alibaba/Sentinel · warning · RequestException

No legal Content-Length

Error message

No legal Content-Length

What it means

The simple-http command center requires every POST to carry a parseable, positive Content-Length header. processPostRequest parses headerMap.get("content-length") with Integer.parseInt (failures silently leave 0) and throws RequestException(StatusCode.LENGTH_REQUIRED, "No legal Content-Length") when bodyLength < 1 — returned to the client as HTTP 411. Without a length it cannot know how many body bytes to read.

Source

Thrown at sentinel-transport/sentinel-transport-simple-http/src/main/java/com/alibaba/csp/sentinel/transport/command/http/HttpEventTask.java:191

            throw new RequestException(StatusCode.BAD_REQUEST, "");
        }

        if (headerMap.containsKey("content-type") && !checkContentTypeSupported(headerMap.get("content-type"))) {
            // not supported Content-type
            CommandCenterLog.warn("Request not supported: unsupported Content-Type: " + headerMap.get("content-type"));
            throw new RequestException(StatusCode.UNSUPPORTED_MEDIA_TYPE,
                "Only form-encoded post request is supported");
        }

        int bodyLength = 0;
        try {
            bodyLength = Integer.parseInt(headerMap.get("content-length"));
        } catch (Exception e) {
        }
        if (bodyLength < 1) {
            // illegal request without Content-length header
            CommandCenterLog.warn("Request not supported: no available Content-Length in headers");
            throw new RequestException(StatusCode.LENGTH_REQUIRED, "No legal Content-Length");
        }

        parseParams(readBody(in, bodyLength), request);
    }

    /**
     * Process header line in request
     *
     * @param in
     * @return return headers in a Map, null for illegal request
     * @throws IOException
     */
    protected static Map<String, String> parsePostHeaders(InputStream in) throws IOException {
        Map<String, String> headerMap = new HashMap<String, String>(4);
        String line;
        while (true) {
            line = readLine(in);
            if (line == null || line.length() == 0) {

View on GitHub (pinned to a3f40ba8e9)

Solutions

  1. Always send Content-Length: use a fixed-size body (curl -d @file or byte[] with the client's default buffering) instead of chunked streaming
  2. Disable chunked transfer in your HTTP client for command-center calls (e.g. Apache HttpClient entity with known length)
  3. If the body is empty, still send Content-Length: 0? No — this endpoint requires a body, so send at least one form parameter

Example fix

// before (chunked, no Content-Length)
HttpPost post = new HttpPost(url);
post.setEntity(new InputStreamEntity(stream, -1)); // -1 -> chunked -> 411

// after
byte[] body = formBytes;
post.setEntity(new ByteArrayEntity(body)); // sets Content-Length automatically
Defensive patterns

Strategy: validation

Validate before calling

// client-side: ensure a fixed-length body so Content-Length is sent
if (bodyBytes == null || bodyBytes.length < 1) {
    throw new IllegalStateException("POST to command center requires a non-empty body");
}
connection.setRequestProperty("Content-Length", String.valueOf(bodyBytes.length));
connection.setChunkedStreamingMode(-1); // never chunked: 0 disables chunking

Try / catch

if (status == 411) {
    // resend with buffered, fixed-length body so Content-Length is present
}

Prevention

When it happens

Trigger: POSTing with chunked transfer encoding (curl -H 'Transfer-Encoding: chunked'), using HTTP/1.0 without Content-Length, or a Content-Length of 0/negative/non-numeric to the simple-http command port; header name case variants are handled, but a missing or malformed value is not.

Common situations: HTTP client libraries defaulting to chunked encoding for streaming bodies; proxies stripping Content-Length; curl with --data-binary on a pipe choosing chunked; empty-body POSTs where the sender omits the header entirely.

Related errors


AI-assisted analysis of alibaba/Sentinel@a3f40ba8e9 (2026-08-14). Data as JSON: /api/errors/834566bf75a478d6. Report an issue: GitHub.