I have a RESTeasy-based REST web service (see below). I'm trying to use the google REST client to execute a request to test my service, but I'm unsure as to how the request should be setup.
I'm not sure how to send the byte[]
as a param (filedata
).
Any ideas on how to test this?
I get the following exception:
java.io.IOException: Unable to get boundary for multipart
with
request:
-content-type=multipart/form-data
-form params:
test=testvalue
Rest method:
@POST
@Path("/upload")
@Consumes("multipart/form-data")
public Response create(@MultipartForm FileUploadForm form) {
System.out.println("form=" + form.getTest());
return null;
}
FileUploadForm Pojo:
import javax.ws.rs.FormParam;
import org.jboss.resteasy.annotations.providers.multipart.PartType;
public class FileUploadForm {
private byte[] filedata;
private String test;
public FileUploadForm() {}
public byte[] getFileData() {
return filedata;
}
@FormParam("filedata")
@PartType("application/octet-stream")
public void setFileData(final byte[] filedata) {
this.filedata = filedata;
}
public String getTest() {
return test;
}
@FormParam("test")
@PartType("application/json")
public void setTest(String test) {
this.test = test;
}
}
Multipart/Form-Data is a popular format for REST APIs, since it can represent each key-value pair as a “part” with its own content type and disposition. Each part is separated by a specific boundary string, and we don't explicitly need Percent Encoding for their values.
Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.
You need to add this header to your request:
Accept-Encoding:multipart/form-data
usually you use Content type like this:
Content-Type: image/png
You can test it with Postman REST client
I've attached an image on how the form should be filled out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With