Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Node.js, PHP and APIs

Spring rejects a JSON byte[] field because the string is not Base64 Jackson accepts

Your error

JSON parse error: Cannot deserialize value of type `byte[]` from String "data:image/png;base64,iVBORw0KGgo...": Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character ':' (code 0x3a) in base64 content2 more lines it can show

Fix it fast

Most common cause, 1 of 3

The client sends a data: URL instead of bare Base64

Cut everything up to and including the first comma before sending, so the field carries only the encoded bytes.

Fix
const reader = new FileReader();
reader.addEventListener('load', () => {
  const base64 = String(reader.result).split(',')[1];
  fetch('/api/files', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ name: file.name, data: base64 }),
  });
});
reader.readAsDataURL(file);
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Jackson turns a JSON string into byte[] only by decoding it as standard, padded Base64 (the MIME-NO-LINEFEEDS variant). It fails when the value starts with a data: URL prefix, uses the URL-safe - and _ characters, has its padding stripped, or is not Base64 at all. Send bare padded Base64 or set a matching Base64Variant. Jackson 2.23 and 3.3.

  • Applies to Jackson databind 3.3.0, Jackson databind 2.23.0, Spring Boot 4.2.0
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Pull the part of the message that names the problem. A ':' points to cause 1, a '-' or '_' to cause 2, and the padding text to cause 3. Any other character means the client is not sending Base64 at all.

Run this first
grep -oE "Illegal character '.' \(code 0x[0-9a-f]+\)|expects padding" app.log | sort | uniq -c

Then find the line you see in this list and open its fix:

  1. 1The client sends a data: URL instead of bare Base64Illegal character ':' (code 0x3a), and the quoted string in the message begins with data:
  2. 2The value uses the URL-safe alphabetIllegal character '-' (code 0x2d) or Illegal character '_' (code 0x5f)
  3. 3The trailing = padding was strippedUnexpected end of base64-encoded String ... expects padding (one or more '=' characters) at the end

What does the error look like?

  • JSON parse error: Cannot deserialize value of type `byte[]` from String "data:image/png;base64,iVBORw0KGgo...": Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character ':' (code 0x3a) in base64 content
  • Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '-' (code 0x2d) in base64 content
  • Unexpected end of base64-encoded String: base64 variant 'MIME-NO-LINEFEEDS' expects padding (one or more '=' characters) at the end. This Base64Variant might have been incorrectly configured

Cause 1 of 3

The client sends a data: URL instead of bare Base64

You seeIllegal character ':' (code 0x3a), and the quoted string in the message begins with data:

Check

Cannot deserialize value of type byte[] with Illegal character ':' means the JSON value holds a whole data: URL, such as data:image/png;base64, followed by the payload. FileReader.readAsDataURL in the browser returns that form, and MDN says the declaration has to be removed before the rest decodes as Base64.

Check
grep -o 'from String "data:[^,]*,' app.log | head -n 3

Fix

Cut everything up to and including the first comma before sending, so the field carries only the encoded bytes.

Fix
const reader = new FileReader();
reader.addEventListener('load', () => {
  const base64 = String(reader.result).split(',')[1];
  fetch('/api/files', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ name: file.name, data: base64 }),
  });
});
reader.readAsDataURL(file);

Verify

The upload returns a 2xx status and the stored byte[] has the same length as the original file.

Verify
curl -s -o /dev/null -w '%{http_code}\n' -H 'Content-Type: application/json' -d '{"name":"a.txt","data":"aGVsbG8="}' http://localhost:8080/api/files

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 3

The value uses the URL-safe alphabet

You seeIllegal character '-' (code 0x2d) or Illegal character '_' (code 0x5f)

Check

Jackson decodes byte[] fields with its default variant, MIME-NO-LINEFEEDS, which keeps the standard + and / characters. Text from a URL-safe encoder replaces those two with - and _, so decoding stops at the first one. Jackson's own URL variant, MODIFIED_FOR_URL, uses - and _ and no padding.

Check
grep -o 'Illegal character .\(-\|_\)' app.log | head -n 3

Fix

Encode with the standard alphabet on the sender, or switch the mapper's default variant. On Spring Boot 4 (Jackson 3), a JsonMapperBuilderCustomizer bean sets it; with Jackson 2, call setBase64Variant on the ObjectMapper. The variant then applies to every byte[] field the mapper reads and writes, and it rejects = padding.

Fix
import org.springframework.boot.jackson.autoconfigure.JsonMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import tools.jackson.core.Base64Variants;

@Configuration
class JacksonBase64Config {
    @Bean
    JsonMapperBuilderCustomizer urlSafeBase64() {
        return builder -> builder.defaultBase64Variant(Base64Variants.MODIFIED_FOR_URL);
    }
}

Verify

The same request with - and _ in the value is accepted, and the decoded bytes match what the sender encoded.

Verify
curl -s -w '\n%{http_code}\n' -H 'Content-Type: application/json' -d '{"name":"a.bin","data":"-_-_"}' http://localhost:8080/api/files

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

The trailing = padding was stripped

You seeUnexpected end of base64-encoded String ... expects padding (one or more '=' characters) at the end

Check

Jackson's built-in variants that write padding also require it when reading. Base64 whose length is not a multiple of four, with the closing = characters dropped by the sender, ends mid-unit and fails with the padding message.

Check
grep -c "expects padding" app.log

Fix

Have the sender keep padding (in Java, avoid Base64.getEncoder().withoutPadding()), or relax the read rule with withPaddingAllowed() on the default variant. Another route is to map the field as String and decode it yourself: java.util.Base64 decoders accept input without padding.

Fix
public record FileUpload(String name, String data) {
    public byte[] bytes() {
        return java.util.Base64.getDecoder().decode(data);
    }
}

Verify

A value such as aGVsbG8 (hello without its =) now decodes to 5 bytes.

Verify
byte[] b = java.util.Base64.getDecoder().decode("aGVsbG8");
System.out.println(b.length); // 5

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Write down in the API contract that byte[] fields carry standard Base64 with padding and no data: prefix, and reject other input in a client-side check.
  • Add one controller test that posts a known value such as aGVsbG8= and asserts the decoded bytes, so a mapper change shows up in CI.
  • If one endpoint needs URL-safe input, use a String field and java.util.Base64.getUrlDecoder() there instead of changing the default variant for the whole app.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro.

Free tool for this

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.