quarkusio/quarkus · error · ErrorDataEncoderException

Cannot add value once finalized

Error message

Cannot add value once finalized

What it means

Once finalizeRequest() has been called, the encoder's headers are written and no further body data can be added; addBodyHttpData throws ErrorDataEncoderException. The encoder is one-shot: finalize is terminal.

Source

Thrown at independent-projects/resteasy-reactive/client/runtime/src/main/java/org/jboss/resteasy/reactive/client/impl/multipart/PausableHttpPostRequestEncoder.java:510

        if (file.length != contentType.length && file.length != isText.length) {
            throw new IllegalArgumentException("Different array length");
        }
        for (int i = 0; i < file.length; i++) {
            addBodyFileUpload(name, file[i], contentType[i], isText[i]);
        }
    }

    /**
     * Add the InterfaceHttpData to the Body list
     *
     * @throws NullPointerException
     *         for data
     * @throws ErrorDataEncoderException
     *         if the encoding is in error or if the finalize were already done
     */
    public void addBodyHttpData(InterfaceHttpData data) throws ErrorDataEncoderException {
        if (headerFinalized) {
            throw new ErrorDataEncoderException("Cannot add value once finalized");
        }
        bodyListDatas.add(checkNotNull(data, "data"));
        if (!isMultipart) {
            if (data instanceof Attribute attribute) {
                try {
                    // name=value& with encoded name and attribute
                    String key = encodeAttribute(attribute.getName(), charset);
                    String value = encodeAttribute(attribute.getValue(), charset);
                    Attribute newattribute = factory.createAttribute(request, key, value);
                    multipartHttpDatas.add(newattribute);
                    globalBodySize += newattribute.getName().length() + 1 + newattribute.length() + 1;
                } catch (IOException e) {
                    throw new ErrorDataEncoderException(e);
                }
            } else if (data instanceof FileUpload fileUpload) {
                // since not Multipart, only name=filename => Attribute
                // name=filename& with encoded name and filename
                String key = encodeAttribute(fileUpload.getName(), charset);

View on GitHub (pinned to e1c734241f)

Solutions

  1. Create a new PausableHttpPostRequestEncoder for each request
  2. Add all attributes and file uploads before calling finalizeRequest
  3. Track a finalized flag in your code and rebuild the encoder when late data arrives

Example fix

// before
encoder.finalizeRequest();
encoder.addBodyAttribute("late", "value");
// after
encoder.addBodyAttribute("late", "value");
encoder.finalizeRequest();
Defensive patterns

Strategy: try-catch

Validate before calling

if (encoder.isFinalized()) { /* create a new encoder before adding data */ }

Type guard

null

Try / catch

try { encoder.addBodyHttpData(data); } catch (ErrorDataEncoderException e) { encoder = rebuildEncoder(request, bodyDataPlus(data)); }

Prevention

When it happens

Trigger: Calling addBodyHttpData, addBodyAttribute or addBodyFileUpload after finalizeRequest() has completed.

Common situations: Reusing a single encoder to send multiple sequential requests; adding a late attribute in a callback after the request was already finalized.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/d5db17d5cf07947f. Report an issue: GitHub.