2

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
    
    try{
        ServletFileUpload upload = new ServletFileUpload();
        FileItemIterator iterator = upload.getItemIterator(request);
        System.out.println("test");
        while (iterator.hasNext()) {
             System.out.println("test2");
            FileItemStream item = iterator.next();
            InputStream stream = item.openStream();

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

                if(item.getFieldName().equals("textBoxFormElement")){
                    System.out.println("text Value : "+pFieldValue);
                }
              }
            }
    }catch (Exception e) {
        e.printStackTrace();
    }
    
}

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 = "";
            System.out.println("test");
            int data = inputStreamReader.read();
            while(data != -1){
                System.out.println("test2");
                char theChar = (char) data;
                System.out.println(theChar);
                allChars = allChars + theChar;
                data = inputStreamReader.read();
            }

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

(still no "test2")...and:

    String requestBody = extractPostRequestBody(request);
    System.out.println(requestBody);


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

    try {
        s = new Scanner(request.getInputStream(), "UTF-8").useDelimiter("\\A");
    } catch (IOException e) {
        e.printStackTrace();
    }
    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

crispy235
  • 39
  • 5

1 Answers1

2

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.

Mark Leusink
  • 3,747
  • 15
  • 23
  • Brilliant - thanks so much Mark - that works perfectly! I still can't fathom why none of my attempts worked or is that simply down to none of them being compatible with multipart/formdata? I assume this method won't work when it comes to handling file upload POSTs which my first example was meant to handle? Anyway I'll obviously accept your answer but any suggestions on capturing files would be appreciated. Thanks again. – crispy235 Aug 14 '20 at 14:06
  • Sorry Mark missed the bit at the bottom.."...or a file that's uploaded." Great! Thank you. – crispy235 Aug 14 '20 at 15:42
  • 1
    @crispy235 - I don't know why your code wouldn't work, but you might try calling `ServletFileUpload.isMultipartContent(request)` to test whether the request is really multipart. Also, I've successfully used the following code with the same `ServletFileUpload` class in non-Domino apps, to get a list of form fields instead of an iterator: `List items=upload.parseRequest(request);` – Scott Leis Aug 17 '20 at 01:56