I am 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 looks 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 contains more parameters and full code, but the annotations are the same. This results in the following exception when passing 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 raised the exception, it looks like the parameter does not pass:
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 the trace, and the name / values ββare different when using javascript compared to the direct HTML message. In the trace for the HTML message, the content type is returned in the download stream:
Request Headers From Upload Stream Content-Length 1756 Content-Type multipart/form-data; boundary=---------------------------1523409566516443041527622966
But does javascript seem to be just a standard message or something else? Any ideas how I replicate a multiformdata message in javascript ??
Any ideas, it seems to me, I pass things through OK? I also tried this using a regular HTML form post and it works great, so it should be related to javascript.