UTF-8 encoding problem with MultiPart/RestEasy

See original GitHub issue

Describe the bug I develop a rest API with MULTIPART_FORM. But I’m having problems with @Consumes charset. I sent some params to the controller which includes German characters. And it didn’t work as expected. The characters are not encoded properly, what can also be seen in debugging the properties in VSCode.

Some of the characters that cause the problem:

  • ä, Ä, ü, Ü, ö, Ö, ß

Instead, I receive:

  • ��

Expected behavior

  • Receive all Umlaut, etc. UTF-8

To Reproduce Using httpie:

http -f POST localhost:8080/general content="Test-Ä" file@testFile.png

Response:

HTTP/1.1 200 OK
Content-Length: 11
Content-Type: text/plain;charset=UTF-8

Test-��

Ressource:

import org.jboss.resteasy.annotations.providers.multipart.MultipartForm;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

public class FeedbackResource {

    @POST
    @Path("/general")
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.MULTIPART_FORM_DATA+";charset=UTF-8")
    public String postForm(@MultipartForm final FeedbackBody feedback) {
        return feedback.content;
    }
}

Model:

package org.acme;

import org.jboss.resteasy.annotations.providers.multipart.PartType;

import javax.ws.rs.FormParam;
import javax.ws.rs.core.MediaType;

public class FeedbackBody {

    private byte[] file;
    public byte[] getFile() {
        return file;
    }

    @FormParam("file")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public void setFile(byte[] file) {
        this.file = file;
    }

    @FormParam("fileName")
    @PartType(MediaType.TEXT_PLAIN)
    public String fileName;

    @FormParam("content")
    @PartType(MediaType.TEXT_PLAIN+";charset=UTF-8")
    public String content;
}

Configuration

# Add your application.properties here, if applicable.
<properties>
    <compiler-plugin.version>3.8.1</compiler-plugin.version>
    <maven.compiler.parameters>true</maven.compiler.parameters>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <quarkus-plugin.version>1.5.1.Final</quarkus-plugin.version>
    <quarkus.platform.artifact-id>quarkus-universe-bom</quarkus.platform.artifact-id>
    <quarkus.platform.group-id>io.quarkus</quarkus.platform.group-id>
    <quarkus.platform.version>1.5.1.Final</quarkus.platform.version>
    <surefire-plugin.version>2.22.1</surefire-plugin.version>
  </properties>

Environment (please complete the following information):

  • Output of uname -a or ver: Darwin Mac.local 19.5.0 Darwin Kernel Version 19.5.0: Tue May 26 20:41:44 PDT 2020; root:xnu-6153.121.2~2/RELEASE_X86_64 x86_64
  • Output of java -version: openjdk version “11.0.6” 2020-01-14 OpenJDK Runtime Environment GraalVM CE 19.3.1 (build 11.0.6+9-jvmci-19.3-b07) OpenJDK 64-Bit Server VM GraalVM CE 19.3.1 (build 11.0.6+9-jvmci-19.3-b07, mixed mode, sharing)
  • Quarkus version or git rev: 1.5.1.Final
  • Build tool (ie. output of mvnw --version or gradlew --version): Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f) Maven home: /Users/.m2/wrapper/dists/apache-maven-3.6.3-bin/1iopthnavndlasol9gbrbg6bf2/apache-maven-3.6.3 Java version: 11.0.6, vendor: Oracle Corporation, runtime: /Users/christian/.sdkman/candidates/java/19.3.1.r11-grl Default locale: en_GB, platform encoding: UTF-8 OS name: “mac os x”, version: “10.15.5”, arch: “x86_64”, family: “mac”

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:18 (9 by maintainers)

github_iconTop GitHub Comments

5reactions
gsmetcommented, Aug 22, 2020

I think we could hardcode it to UTF-8 and see if people want to make it configurable. UTF-8 is certainly a better default than the current situation.

4reactions
eviallecommented, Aug 22, 2020

I solved that issue by adding an Interceptor in my code

package fr.vstudios.leclick.front;

import org.jboss.resteasy.plugins.providers.multipart.InputPart;

import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.ext.Provider;


@Provider
public class CharsetInterceptorFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext context) {
        context.setProperty(InputPart.DEFAULT_CHARSET_PROPERTY, "UTF-8");
    }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to set encoding in RESTeasy to UTF-8? - Stack Overflow
I believe that default encoding in RESTeasy is us-ascii. Do you know how to change it to UTF-8? Maybe there is another solution...
Read more >
Better handling of UTF-8 encoding in multipart/form-data
My client is uploading a file as a multipart/form-data object, where the file being uploaded has a MIME type specified (in my case,...
Read more >
UTF-8 Character Debug Tool - I18nQA
Unicode Windows 1252 Expected Actual UTF‑8 Bytes Unicode Windows 1252 Expect... U+20AC 0x80 € € %E2 %82 %AC U+00C0 0xC0 À 0x81 U+00C1 0xC1 Á U+201A...
Read more >
What is UTF-8 Encoding? A Guide for Non-Programmers
Enter Unicode, an encoding system that solves the space issue of ASCII. Like ASCII, Unicode assigns a unique code, called a code point, ......
Read more >
How can I fix the UTF-8 error when bulk uploading users?
The file should now be in UTF-8 encoding, and it will successfully upload. If you use Microsoft Excel. Open your CSV file in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found