Despite having a functional Spring Framework API setup for receiving GET/POST requests on the web, I encountered an issue when attempting to post from an Android device. The error message "Required String parameter 'firstname' is not present" kept appearing.
I tried various iterations of the POST method but consistently faced the same error.
This involves Java Spring Framework:
@RequestMapping(value = "/addUser", method = RequestMethod.POST)
public String addUser(@RequestParam(value="firstname") String firstname,
@RequestParam(value="lastname") String lastname,
@RequestParam(value="username") String username,
@RequestParam(value="password") String password) throws Exception {
String success = add.addUser(firstname, lastname, username, password);
return toJson(success);
}
Here's the JavaScript version:
var data = {
firstname: firstname,
lastname: lastname,
username : username,
password : password
};
$.ajax({
type: 'POST',
url: "<url removed>/import/addUser",
success: function(data) {
callbackSuccess(JSON.stringify(data));
},
error: function(data) {
callbackFail(data);
},
data: data,
dataType: "json"
});
Now let's talk about the Android implementation:
String url = "<url removed>/import/addUser";
InputStream inputStream = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.accumulate("firstname", userFirstName);
jsonObject.accumulate("lastname", userLastName);
jsonObject.accumulate("username", emailStr);
jsonObject.accumulate("password", passwordStr);
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if(inputStream != null)
result = convertInputStreamToString(inputStream);
else
result = "Did not work!";
Log.i("Response", result);
Any suggestions or insights are welcome!
UPDATE: I made adjustments to the Spring Framework code to remove the parameters' required status, resulting in only null values being received instead of the actual variables. This leads me to believe that the issue lies with the POST mechanism itself rather than the server-side configuration.