2

I have an endpoint https://www..com

When I make a curl call, I have the endpoint as https://www..com?param1=true

I want to do a similar call from Nodejs, I am not sure if param1 should be passed in headers, concatenated to the endpoint or passed in options. What is the right way to do so?

My Node JS looks like this to make calls to my Node Server amd my file looks as follows,

    app.post('/thisway', function(req, res){
    var ENDPOINT = req.body.endPoint 
//(this gets me till https://<url> part of the endpoint string)
        var urlToHit = ENDPOINT.concat("?param1=true")
    var headers = {
            'Authorization': xxxxx,
            'Accept': '*/*',
            'X-Spark-Service-Instance': xxxxx
        }


    var options= {
        url: urlToHit,
        headers: headers,
        json: {xxxxxx}

          }
       request(options, callback);
}
Nagireddy Hanisha
  • 1,290
  • 4
  • 17
  • 39

3 Answers3

2

When doing a post it is not necessary to add a query string parameter in the route post. Mostly used for app.get. You can add the details in the JSON string data that you are sending. You can then use the req.body or req.query to get the item. However you can do it this way:

 app.post('/thisway/:variable', function(req, res){

Then you retrieve the parameter using req.param.variable

Good EXAMPLE

Conrad Lotz
  • 8,200
  • 3
  • 23
  • 27
  • Thanks a lot :) But the problem I am facing is something else, I have updated the question to reflect my problem better. Apologies for the inconvenience – Nagireddy Hanisha May 31 '17 at 08:21
1

You can pass it as you have shown in your example in urlToHit. You don't have to pass it in header or options.

var urlToHit = ENDPOINT.concat("?param1=true")

This should complete the request with the needed parameters. Since even when you are doing a curl call, this is the endpoint you hit, it should be the same endpoint here as well.

Dreams
  • 5,854
  • 9
  • 48
  • 71
0

In your angularjs make a post request to /thisway?variable=true rather than /thisway and in your router you can configure as following:

app.post('/thisway', function(req, res){
    var variable = req.query.variable;

    if (variable) {
    //do something
    } else {
    //do something
    }
});
Gnanesh
  • 668
  • 7
  • 13
  • Thank you :) But the problem I am facing is something else, I have updated the question to reflect my problem better. Apologies for the inconvenience – Nagireddy Hanisha May 31 '17 at 08:22