尝试在 Rest Assured 中发送图像,为什么没有发送任何内容? , 即使没有例外

Trying to send an image in Rest Assured , Why is nothing being sent ? , even not getting exception

我正在测试 API 使用放心,编程语言是 JAVA,试图发送图像,但没有发送任何内容;

我有以下代码

@Test
public void uploadFile() {
   

     File testUploadFile = new File("C:\Users\######\Downloads250987.jpeg");
     
        RestAssured.baseURI = "http://localhost:8080";
     
        Response response = given()
            .multiPart(testUploadFile)
            .when().
        post("/api/product/zOPePEpj/file");
     
     
        System.out.println(response.getStatusCode());
        System.out.println(response.asString());
              
}
      
                        

实际响应正文是

 {
    "productImage": {
        "id": null,
        "productId": null,
        "path": null,
        "pathSmall": null,
        "pathMedium": null,
        "pathLarge": null,
        "name": null,
        "description": null,
        "isMainImage": false,
        "translations": [
            
        ]
    },
    "error": null,
    "errorCode": 0
}

预期的响应正文是

{
  "productImage": {
    "id": "Kdl2V4lX",
    "productId": "zOPePEpj",
    "path": "https://bookitdev.s3.amazonaws.com/3831/images/1624_6b6e64b15d9b49f7bc78a9223c4c49cb_original",
    "pathSmall": "https://bookitdev.s3.amazonaws.com/3831/images/1624_0e1fa879180b4916961867d32347f646_small",
    "pathMedium": "https://bookitdev.s3.amazonaws.com/3831/images/1624_b06c93a11b20408db2d9e4eff6e2c13b_medium",
    "pathLarge": "https://bookitdev.s3.amazonaws.com/3831/images/1624_1b21a3e825af41b39cfffacfeef7e054_large",
    "name": null,
    "description": null,
    "isMainImage": false,
    "translations": []
  },
  "error": null,
  "errorCode": 0
}

请帮我解决这个问题,在此先感谢

您可以尝试其他 2 种重载方法。

RequestSpecification multiPart(String controlName, File file);

例如

Response response = given()
        .multiPart("image", testUploadFile)
        .when().
        post("/api/product/zOPePEpj/file");

RequestSpecification multiPart(String controlName, File file, String mimeType);

例如:

Response response = given()
        .multiPart("image", testUploadFile, "image/jpeg")
        .when().
        post("/api/product/zOPePEpj/file");

如果能抓到正确请求的log,再构建Rest-assured request就更好了

尝试使用 contentType("multipart/form-data") 如下:

    Response response = given()
        .contentType("multipart/form-data")
        .multiPart(testUploadFile)
        .when()
        .post("/api/product/zOPePEpj/file");