在放心中上传 csv 文件返回 500

问题描述 投票:0回答:1

我一直在努力通过 Rest Assured 上传我的 csv 文件。它返回 500,而它在 Postman 上运行良好。

File myFile = new File("src\test\resources\myfile.csv");

    response = given()
            .spec(requestSpecification).config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().appendDefaultContentCharsetToContentTypeIfUndefined(false)))
            .multiPart("file", myFile,"multipart/form-data")
            .when()
            .post(endpoint)
            .then().extract().response();

我也尝试过

  • 不带:encoderConfig 和appendDefaultContentCharsetToContentTypeIfUndefined

  • 与:

          .body(file)
          .contentType("text/csv")
    

还有,

.config(RestAssured.config().encoderConfig(EncoderConfig.encoderConfig().encodeContentTypeAs("multipart/form-data", ContentType.MULTIPART)))
                .contentType("multipart/form-data; boundary=--MyBoundary")

我想我已经尝试了在互联网上找到的所有内容,但它仍然返回 500。

在 Postman 上效果很好 我主要关注 multipart/contentType 部分,但也许问题出在其他地方?

您还有其他想法吗?我将非常感激

api multipartform-data rest-assured multipart
1个回答
0
投票

如果选择这种方式

multiPart(String controlName, File file, String mimeType)
上传,那么最后一个参数就是mimeType "text/csv"

.multiPart("file", myFile, "text/csv");

完整代码:

File myFile = new File("src\test\resources\myfile.csv");

response = given()
    .spec(requestSpecification)
    .multiPart("file", myFile, "text/csv")
    .when()
    .post(endpoint)
    .then().extract().response();
© www.soinside.com 2019 - 2024. All rights reserved.