flowable/flowable-engine · error · FlowableIllegalStateException

Cannot set both body and body bytes

Error message

Cannot set both body and body bytes

What it means

HttpRequest forbids combining a String body with raw bodyBytes. Since the two body representations are mutually exclusive, setBody throws FlowableIllegalStateException if bodyBytes has already been set. This prevents ambiguity about which payload to send.

Solutions

  1. Choose one representation: remove the setBodyBytes call if you want a String body (or vice versa)
  2. Create a new HttpRequest instance instead of reusing one configured for a different payload type
  3. Extract request-building into a single code path that sets exactly one body form

Example fix

// before
request.setBodyBytes(data);
request.setBody("{\"ok\":true}"); // throws
// after
request.setBodyBytes(data); // keep bytes only
// or: request.setBody("{\"ok\":true}"); // keep string only
Defensive patterns

Strategy: validation

Validate before calling

// guard before setting a String body
if (request.getBodyBytes() != null) {
    throw new IllegalStateException("Request already has bodyBytes; pick one payload type");
}
request.setBody(body);

Type guard

boolean canSetStringBody(HttpRequest r) { return r.getBodyBytes() == null; }

Try / catch

try {
    request.setBody(body);
} catch (FlowableIllegalStateException e) {
    request = new HttpRequest(); // rebuild with a single payload type
    request.setBody(body);
}

Prevention

When it happens

Trigger: Calling request.setBody(jsonString) after a previous call to request.setBodyBytes(byteArray) on the same HttpRequest instance.

Common situations: Building one request object across code paths where one branch sets bytes (file upload) and another sets a String (JSON payload); refactoring code that switched body representations without clearing the other.

Related errors


AI-assisted analysis of flowable/flowable-engine@d6d39ce1c6 (2026-09-11). Data as JSON: /api/errors/8ffd66516ecce6fb. Report an issue: GitHub.

Appendix: source

Thrown at modules/flowable-http-common/src/main/java/org/flowable/http/common/api/HttpRequest.java:103

    public HttpHeaders getSecureHttpHeaders() {
        return secureHttpHeaders;
    }

    public String getSecureHttpHeadersAsString() {
        return secureHttpHeaders != null ? secureHttpHeaders.formatAsString(true) : null;
    }

    public void setSecureHttpHeaders(HttpHeaders secureHttpHeaders) {
        this.secureHttpHeaders = secureHttpHeaders;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        if (bodyBytes != null) {
            throw new FlowableIllegalStateException("Cannot set both body and body bytes");
        } else if (multiValueParts != null && !multiValueParts.isEmpty()) {
            throw new FlowableIllegalStateException("Cannot set both body and multi value parts");
        } else if (formParameters != null && !formParameters.isEmpty()) {
            throw new FlowableIllegalStateException("Cannot set both body and form parameters");
        }
        this.body = body;
    }

    public String getBodyEncoding() {
        return bodyEncoding;
    }

    public void setBodyEncoding(String bodyEncoding) {
        this.bodyEncoding = bodyEncoding;
    }

    public byte[] getBodyBytes() {
        return bodyBytes;

View on GitHub (pinned to d6d39ce1c6)