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
Advertisement
Answer
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.