javarest-assured

Rest-assured POST call with multipart file that contains Cyrillic characters in the filename


I am sending POST call to an endpoint with file that does have Cyrillic characters in the filename. I have tried setting encoding to UTF-8 in every place I knew about.

RestAssured.config = RestAssured.config()
        .encoderConfig(encoderConfig().defaultContentCharset("UTF-8"))
        .encoderConfig(encoderConfig().defaultCharsetForContentType("UTF-8", "multipart/form-data"))
        .multiPartConfig(multiPartConfig().defaultCharset("UTF-8"))
        .decoderConfig(decoderConfig().defaultContentCharset("UTF-8"))
        .decoderConfig(decoderConfig().defaultCharsetForContentType("UTF-8", "multipart/form-data"));

RequestSpecification rs = given()
        .baseUri(baseUrl)
        .header("Content-Type", "multipart/form-data")
        .header(getAuthHeader())
        .config(RestAssured.config)
        .multiPart(
                new MultiPartSpecBuilder(file)
                        .controlName("file")
                        .mimeType("audio/mpeg")
                        .fileName(file.getName())
                        .charset("UTF-8")
                        .build()
        )
        .multiPart(
                new MultiPartSpecBuilder(categoryId)
                        .controlName("categoryId")
                        .build()
        )
        .multiPart(
                new MultiPartSpecBuilder(fileTitle)
                        .charset(Charsets.UTF_8)
                        .controlName("fileTitle")
                        .build()
        )
        .log().everything();

rs
    .when()
        .post(FilePaths.add.value)
    .then()
        .log().everything()
        .statusCode(201)
        .contentType(ContentType.JSON);

When I am debugging an API code, I can see that in above case content-disposition header comes with Cyrillic characters replaced with question mark: form-data; name="file"; filename="??????.mp3".

When I make same call with Postman, it works just fine. The only difference I see between rest-assured call and Postman call is the content-disposition header.

Any idea what needs to be tweaked for rest-assured configuration?


Solution

  • I found an answer by looking at logged rest-assured issue here.

    I just had to make sure I am not using STRICT mode (which is the default):

    .config(RestAssuredConfig.config().httpClient(HttpClientConfig.httpClientConfig().httpMultipartMode(HttpMultipartMode.BROWSER_COMPATIBLE))