I am creating an app which uses some node.js scripts for server scripting mainly because node.js has Firebase
support, but for some reason I am unable to send any params with my request (both GET and POST).
My android code is as follows
private void sendData(final String name){
StringRequest sr = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(),response.toString()+" returned from node.js server",Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),error.toString()+"The Server returned error",Toast.LENGTH_SHORT).show();
}
}){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String,String> map = new HashMap<>();
map.put("test",name);
return map;
}
};
RequestQueue r = Volley.newRequestQueue(getApplicationContext());
r.add(sr);
}
The corresponding node.js script is as follows
app.get('/hello', function(request, response){
var outputJson = request.params.test;
response.send(JSON.stringify(outputJson));
console.log(outputJson);
})
The console always logs undefined for some reason. Following suggestions from this post I also tried including the data in the body of the request and get the data from the request in the node.js via request.body.test
.
I would like to know what is going wrong and most importantly I would also like to know how to recieve and process POST
request. I read on some blog that I have to implement a middleware
of some kind. Some clarification in that area will also be appreciated.