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

Viewed 139

I'm testing API using rest assured , programming language is JAVA, trying to send an image , but nothing is being sent;

I have following code

@Test
public void uploadFile() {
   

     File testUploadFile = new File("C:\Users\######\Downloads\38250987.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());
              
}
      
                        

Actual Response Body is

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

Expected Response Body is

{
  "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
}

Please help me to resolve this issue , thanks in advance

2 Answers

You could try 2 other overload methods.

RequestSpecification multiPart(String controlName, File file);

for example

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

or

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

for example:

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

It's better if you can capture the log of correct request, then build Rest-assured request

Try with contentType("multipart/form-data") as below:

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