0

Post call for file upload not working as expected using java.I need to upload a file using rest call..The file format is correct and its working perfectly in postman and ui side also but in java its giving "Incorrect file format" as the file is not getting uploaded it seems. Am i missing any header or anything.

File file = new File("/Users/surya/Downloads/2021-06-16.xlsx");
        
          Response response = RestAssured
                    .given()
                    .multiPart("file", file, "multipart/form-data")                
                    .post("http://myuploadsite.com/upload/feedfile");         
            System.out.println(response.asString());

My postman curl request

curl --location --request POST 'http://myuploadsite.com/upload/feedfile' \
--header 'Content-Type: multipart/form-data; boundary=jena' \
--header 'Host;' \
--form 'file=@"/Users/surya/Downloads/2021-06-16.xlsx"'
2
  • inside body what to write?? Commented Jul 2, 2021 at 7:34
  • no. its not working..getting same eroor..can i call directly the postman curl command Commented Jul 2, 2021 at 7:42

1 Answer 1

4

Method you're using is:

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

You need to define mimeType for file you're uploading, in this case is application/vnd.openxmlformats-officedocument.spreadsheetml.sheet or application/vnd.ms-excel. I'm not quite sure which one.

or just leave blank to use overload method:

RequestSpecification multiPart(String controlName, File file);

Code would be:

File file = new File("/Users/surya/Downloads/2021-06-16.xlsx");
        
Response response = RestAssured
      .given()
      .multiPart("file", file, "application/vnd.ms-excel")                
      .post("http://myuploadsite.com/upload/feedfile");         
System.out.println(response.asString());

Content-Type: multipart/form-data will be automatically defined by Rest-Assured when you use multipart

https://github.com/rest-assured/rest-assured/wiki/Usage#multi-part-form-data

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.