I'm currently attempting to send form data using JavaScript to a Jersey Resource. The JavaScript code I have is as follows:
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 structure of the Jersey Resource is shown below:
@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";
}
This example showcases a simplified version with only two parameters, while the actual implementation involves more params and additional code. Despite this, the annotations remain unchanged. However, when testing on Tomcat, an exception is thrown:
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)
By examining the source causing the exception, it appears that the parameter is not being transmitted successfully:
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 }
In my investigation using Firebug, I noticed differences in how names and values are handled between JavaScript and conventional HTML posts. The content type in the HTML request headers is structured within an upload stream:
Request Headers From Upload Stream
Content-Length 1756
Content-Type multipart/form-data; boundary=---------------------------1523409566516443041527622966
However, the JavaScript method seems to perform a standard post. Is there a way to replicate the multi-form data post behavior using JavaScript?
Despite creating what seems like successful passes, I am encountering issues. When testing with a regular HTML form submission, everything functions correctly. It appears the problem lies specifically with the JavaScript approach.