1

I am trying to upload csv file using rest assured multipart upload method. In swagger API documentation, request body is like below for multipart csv upload.

{
"file":"string"
}

My code is like this.

String file = "src/resources/ccp/csvs/sample.csv"
    responsePost = given()
                .multiPart("file", new File(file), "text/csv" )
//                .header("Content-type", "application/json")
                .and()
                .when()
                .post(url)
                .then()
                .extract().response();

This is working in IntelliJ idea without any issue. However when I package the code into a jar using maven and run it, It returns fileNotFound exception.

As I saw, the only solution is to use java file inputstream, but since swagger says to use a file, it is impossible to use input stream, isn't it?

Is there another way to handle this issue?

4
  • You should be able to use this same code in a .jar file if the file is external to the .jar. You just have to get the path right. If the file is in the .jar, then you can't use a simple filesystem path, as there isn't such a thing to the file you want to read. There are a number of options. I would expect that Rest-assured would be able to read upload parts from a stream rather than having to provide a file path. If a file path is necessary, then you could copy the file in the jar file out to a temporary file on the filesystem and then provide the path to that file to the API call. Commented Jan 16, 2022 at 3:39
  • This answer was helpful. I put csv files out of jar file. Thank You CryptoFool Commented Jan 16, 2022 at 15:28
  • Was my answer helpful or just my comment? If my answer was helpful, please accept and/or upvote it. Commented Jan 16, 2022 at 17:16
  • Comment was helpful. Commented Jan 17, 2022 at 10:06

1 Answer 1

1

Per the Rest-Assured Javadocs for the RequestSpecification class, found here:

https://www.javadoc.io/static/com.jayway.restassured/rest-assured/2.9.0/com/jayway/restassured/specification/RequestSpecification.html

there are a total of 13 overloads of the .multiPart method. A few of them take an InputStream. Since you are specifying a MIME type as a parameter, you want this version I believe:

multiPart(String controlName, String fileName, InputStream stream, String mimeType)

So if you have an InputStream rather than a file path, using this version of the method should give you what you want. Just note that in addition to the InputStream, you have to supply a file name, since an InputStream object does not have a file name associated with it like a file on disk does.

If your file is not in your .jar file but is on disk, then you should be able to use the version of the multiPart method that takes a file path. You just need to supply the correct path to the file. The best way to do this is to compute an absolute path rather than making any assumption about what the base directory is, which you have to do if you supply a relative path like in your example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.