Unable to get a handle on the body of a multipart/form-data POST request in XPage ServiceBean

Tags: , , ,

I have an xpage based on a bootstrap framework which uses jQuery $post to submit forms contained within the page. These get posted using multipart/form-data content-type so I have no control over that. I have set up a REST.xsp containing a RESTService which calls a java ServiceBean. The latest method I have tried is as follows:

private void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // TODO Auto-generated method stub
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iterator = upload.getItemIterator(request);
        while (iterator.hasNext()) {
            FileItemStream item = iterator.next();
            InputStream stream = item.openStream();

            if (item.isFormField()) {
                byte[] str = new byte[stream.available()];
                String pFieldValue = new String(str,"UTF8");
                System.out.println("text Value : "+pFieldValue);

                    System.out.println("text Value : "+pFieldValue);
    }catch (Exception e) {

but I just can’t seem to get the server to pick up the POSTed form data. I can see the “test” on the console but it’s not hitting “test2”.

I have tried every suggestion I could find including:

    try {
            InputStream inputStream = request.getInputStream(); 
            Reader inputStreamReader = new InputStreamReader(inputStream);
            String allChars = "";
            int data = inputStreamReader.read();
            while(data != -1){
                char theChar = (char) data;
                allChars = allChars + theChar;
                data = inputStreamReader.read();

         catch (IOException e) {
          System.out.println("No body data readable: " + e.getMessage() );

(still no “test2”)…and:

    String requestBody = extractPostRequestBody(request);

  private static String extractPostRequestBody(HttpServletRequest request) {
    Scanner s = null;

    try {
        s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\A");
    } catch (IOException e) {
    return s.hasNext() ? s.next() : "";

(nothing printed to console).

I have also tried using a REST client to post some multipart form data values to the URL but again nothing is returned. However if I try POSTing some json using application/json content-type and use the last method shown above I can see the json getting printed to the console.

Is there maybe some issue with using the extLib REST control and a java servicebean with multipart POSTs? I am aware that you can only read the request stream once so that previous use of eg request.properties will render the stream empty thereafter but this is not the case and presumably the json wouldn’t display either if it were.

Many thanks


If it’s multipart/formdata that’s send with the POST request to the ExtLib Rest control, you can get a handle to the submitted data like this:

for (Object e : req.getParameterMap().entrySet()) {
    Map.Entry entry = (Map.Entry) e;                
    System.out.println("got " + entry.getKey() + "/" + entry.getValue().getClass().getCanonicalName());

The entries in the map can either be the form fields or a file that’s uploaded.

Source: stackoverflow