Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
    }   
}
like image 751
c12 Avatar asked Apr 19 '11 22:04

c12


People also ask

Does REST API support multipart form data data format?

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.

How do you use multipart form data?

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.


1 Answers

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

like image 55
delkant Avatar answered Oct 10 '22 22:10

delkant