简体   繁体   中英

How to upload a CSV file using REST-assured in Java?

I want to pass a CSV file type to an API in order to execute a POST and PUT requests. I'm using rest-assured and I tried to pass the file with the multiPart() method but it returns always HTTP code 400. Http code 200 is expected and a JSON with details as response.

Manually this is how the request looks like

curl -X POST "https://awesome/url" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "callbackUrl=https://sameAwesome/url" -F "file=@fileName.csv;type=application/vnd.ms-excel" -F "fileFormat="csv"

The below should help

{
    String response = RestAssured.given().multiPart("file2", new File("C:\\Users\\alpha\\Desktop\\Test.csv")).
    when().post("http://localhost:3000/posts").then().extract().asString();
    System.out.println("Response is : " + response);
}

Reference link : https://blog.jayway.com/2011/09/15/multipart-form-data-file-uploading-made-simple-with-rest-assured/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM