Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript Form Post with Jersey MultiFormData - Null Pointer Exception

I'm trying to post some form data using javascript against a Jersey Resource. Here is the javascript:

            var form = document.getElementById('form');
            var formdata = new FormData(form);

            if (window.XMLHttpRequest)
              {// code for IE7+, Firefox, Chrome, Opera, Safari
              xmlhttp=new XMLHttpRequest();
              }
            else
              {// code for IE6, IE5
              xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
              }
            xmlhttp.onreadystatechange=function()
              {
              if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("xmlTextBox").innerHTML=xmlhttp.responseText;
                }
              }
            xmlhttp.open("POST", "PostXml", true);
            xmlhttp.setRequestHeader('Content-Type', 'multipart/form-data');
            xmlhttp.setRequestHeader("Content-length", formdata.length);
            xmlhttp.setRequestHeader("Connection", "close");
            xmlhttp.send(formdata);

The Jersey Resource look like this:

@Path("/Resource")
public class MyClass {

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_XML)
public String postXML(@FormDataParam("param1") String param1, 
@FormDataParam("param2") String param2){

return "test";

}

The real version includes more params and full code, but the annotations are the same. This produces the following exception when running through tomcat:

java.lang.NullPointerException
    at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.unquoteMediaTypeParameters(MultiPartReaderClientSide.java:227)
    at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readMultiPart(MultiPartReaderClientSide.java:154)
    at com.sun.jersey.multipart.impl.MultiPartReaderServerSide.readMultiPart(MultiPartReaderServerSide.java:80)
    at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:144)
    at com.sun.jersey.multipart.impl.MultiPartReaderClientSide.readFrom(MultiPartReaderClientSide.java:82)
    at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:488)
    at com.sun.jersey.spi.container.ContainerRequest.getEntity(ContainerRequest.java:552)

From looking at the source that produced the exception it looks like the param isn't coming through:

224     for (final String parameterName : parameters) {
225         String parameterValue = mediaType.getParameters().get(parameterName);
226
227         if (parameterValue.startsWith("\"")) {
228             parameterValue = parameterValue.substring(1, parameterValue.length() - 1);
229             unquotedParams.put(parameterName, parameterValue);
230         }
231     }

I;ve used firebug to put a trace on and the name / values are coming through differently when using javascript compared to a straight HTML post. In the trace for the HTML post the content type is returned in an upload stream:

Request Headers From Upload Stream
Content-Length  1756
Content-Type    multipart/form-data; boundary=---------------------------1523409566516443041527622966

But the javascript seems to be just a standard post or something? Any ideas how I replicate the multiformdata post in javascript??

Any ideas as it looks like i'm passing things through OK? I've also tried this using a normal HTML form post and it works fine, so must be related to the javascript.

like image 718
thomascrabs Avatar asked May 13 '13 21:05

thomascrabs


People also ask

How to post with multipart form data using FETCH and JavaScript?

Then we call fetch to make a post request to the url with body set to formData. To POST with multipart form data using fetch and JavaScript, we can send a FormData object as its body. ← How to conditionally pass prop to component inline with React? → How to create structs in JavaScript?

Can we handle HTTP POST data from JavaScript?

We can not handle HTTP POST data from JavaScript, but we can still access the data while it is in the HTML form. By. Jacob When working with HTML forms on the client-side, within a browser, it is not technically possible to handle data submitted via a HTTP POST request.

How to throw a missingfileexception in Jersey exceptionmapper?

Now you can throw MissingFileException when ever user requested file is not found in desired location. throw new MissingFileException (fileName + " does not existing on this server !!"); throw new IOException ("Error while reading file :: '"+fileName+"' !!"); 3. Jersey exception handling example Now is the time to test jersey exceptionmapper.

How to prevent the browser from accepting form submissions in JavaScript?

But, JavaScript does have a method called preventDefault which we can use to prevent the default behavior of the browser when a form is submitted. I already covered this topic in this article: Handling HTML Forms in JavaScript. Here is a simple script that you can use to test most HTML forms doing development.


1 Answers

Remove those setRequestHeader

var form = document.getElementById('form');
var formdata = new FormData(form);

var xmlhttp=new XMLHttpRequest();
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        document.getElementById("xmlTextBox").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("POST", "PostXml", true);
xmlhttp.send(formdata);

http://jsfiddle.net/8NWB7/ working
http://jsfiddle.net/8NWB7/1/ not working

like image 63
Musa Avatar answered Oct 14 '22 01:10

Musa