I need to attach a file to my service end-point . I tested the functionality via POSTMAN ( chrome browser plugin to test rest service ) , it is working fine.
But I need to test the same with JUNIT . For that case I am using RESTeasy client .
I was trying with this code :
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new FileReader("C:/Temp/tempfile.txt"));
try {
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
}
finally {
br.close();
}
byte[] file = sb.toString().getBytes();
Client client = ClientBuilder.newClient();
Invocation.Builder builder = client.target(webTarget.getUri()
+ "/attachment" ).request(MediaType.MULTIPART_FORM_DATA_TYPE);
Response response = builder.post(Entity.entity(file, MediaType.MULTIPART_FORM_DATA), Response.class);
But I am getting an error :
org.apache.commons.fileupload.FileUploadException : the request was rejected because no multipart boundary was found
Is there any solution for this ?
Or can anybody give a sample RESTeasy rest client code to attach a file ?