Rest-assured POST 使用文件名中包含西里尔字符的多部分文件调用
Rest-assured POST call with multipart file that contains Cyrillic characters in the filename
我正在向端点发送 POST 调用,该端点的文件名中确实包含西里尔字符。我已经尝试在我知道的每个地方都将编码设置为 UTF-8。
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);
当我调试 API 代码时,我可以看到在上述情况下 content-disposition header 带有用问号替换的西里尔字符:form-data; name="file"; filename="??????.mp3"
。
当我与 Postman 进行相同的呼叫时,它工作正常。我在 rest-assured 调用和 Postman 调用之间看到的唯一区别是 content-disposition header.
知道 rest-assured 配置需要调整什么吗?
我通过查看记录的放心问题找到了答案 here。
我只需要确保我没有使用 STRICT 模式(这是默认模式):
.config(RestAssuredConfig.config().httpClient(HttpClientConfig.httpClientConfig().httpMultipartMode(HttpMultipartMode.BROWSER_COMPATIBLE))
我正在向端点发送 POST 调用,该端点的文件名中确实包含西里尔字符。我已经尝试在我知道的每个地方都将编码设置为 UTF-8。
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);
当我调试 API 代码时,我可以看到在上述情况下 content-disposition header 带有用问号替换的西里尔字符:form-data; name="file"; filename="??????.mp3"
。
当我与 Postman 进行相同的呼叫时,它工作正常。我在 rest-assured 调用和 Postman 调用之间看到的唯一区别是 content-disposition header.
知道 rest-assured 配置需要调整什么吗?
我通过查看记录的放心问题找到了答案 here。
我只需要确保我没有使用 STRICT 模式(这是默认模式):
.config(RestAssuredConfig.config().httpClient(HttpClientConfig.httpClientConfig().httpMultipartMode(HttpMultipartMode.BROWSER_COMPATIBLE))