Use REST client to call multipart/form-data Rest web service

c12 picture c12 · Apr 20, 2011 · Viewed 18.5k times · Source

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;
    }   
}

Answer

delkant picture delkant · Mar 3, 2014

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.

postman multipart/form-data