HttpPost request unsuccessful
- by The Thom
I have written a web service and am now writing a tester to perform integration testing from the outside. I am writing my tester using apache httpclient 4.3. Based on the code here: http://hc.apache.org/httpcomponents-client-4.3.x/quickstart.html and here: http://hc.apache.org/httpcomponents-client-4.3.x/tutorial/html/fundamentals.html#d5e186 I have written the following code.
Map<String, String> parms = new HashMap<>();
parms.put(AirController.KEY_VALUE, json);
postUrl(SERVLET, parms);
...
protected String postUrl(String servletName, Map<String, String> parms)
throws AirException{
String url = rootUrl + servletName;
HttpPost post = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
for(Map.Entry<String, String> entry:parms.entrySet()){
BasicNameValuePair parm = new BasicNameValuePair(entry.getKey(), entry.getValue());
nvps.add(parm);
}
try {
post.setEntity(new UrlEncodedFormEntity(nvps));
} catch (UnsupportedEncodingException use) {
String msg = "Invalid parameters:" + parms;
throw new AirException(msg, use);
}
CloseableHttpResponse response;
try {
response = httpclient.execute(post);
} catch (IOException ioe) {
throw new AirException(ioe);
}
String result;
if(HttpStatus.SC_OK == response.getStatusLine().getStatusCode()){
result = processResponse(response);
}
else{
String msg = MessageFormat.format("Invalid status code {0} received from query {1}.",
new Object[]{response.getStatusLine().getStatusCode(), url});
throw new AirException(msg);
}
return result;
}
This code successfully reaches my servlet. In my servlet, I have (using Spring's AbstractController):
protected ModelAndView post(HttpServletRequest request, HttpServletResponse response) {
String json = String.valueOf(request.getParameter(KEY_VALUE));
if(json.equals("null")){
log.info("Received null value.");
response.setStatus(406);
return null;
}
And this code always falls into the null parameter code and returns a 406.
I'm sure I'm missing something simple, but can't see what it is.