I'm attempting to send a basic POST request from my Android app to a Node.js application using Volley. Below is the code snippet from the Android side:
StringRequest stringRequest=new StringRequest(Request.Method.POST, constants.link,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(getApplicationContext(), response, Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),error.getMessage(),Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<>();
params.put("number","my-phone-number");
params.put("text","Hello World");
return params;
}
};
RequestQueue requestQueue= Volley.newRequestQueue(menu.this);
requestQueue.add(stringRequest);
The above code successfully sends a POST request to a PHP web API, but it doesn't pass parameters when accessing endpoints in my NODE.js application.
Below is the code snippet from my Node.js Application:
var app=express();
var bodyParser=require('body-parser');
const port = 3001
var telnyx = require('telnyx')(MY_API_KEY);
var count=0;
var access=0;
app.use(bodyParser.json());
app.get("/app",function(req,res,next){
console.log(req.query.number);
console.log(req.query.text);
res.send("I am get method"+access);
access++;
});
app.post("/app",function(req,res,next){
sendSMS(req.query.number,req.query.text);
res.send("I am post method "+ access + " " + req.query.number + " " + req.query.text);
access++;
});
app.listen(port,function(){
console.log("Started on port "+port);
});
function sendSMS(num,text) {
console.log("inside function"+num+" "+text);
telnyx.messages.create(
{
'from': '+15074077011', // Your Telnyx number
'to': '+'+ num,
'text': text
},
function (err, response) {
console.log("message sent "+text+" "+num);
count++
}
);
}
Now, sending a POST request from POSTMAN to the NODE.js application works fine and I receive a message on my mobile phone. However, when trying to send a POST request from the Android app with parameters, the parameters are not received correctly on the NODE.js application.
1). Is there a difference between the POST request from POSTMAN and that from Volley in Android?
2). How can I modify the Android code to ensure proper passing of parameters to the NODE.js application?