Nagireddy Hanisha
Nagireddy Hanisha

Reputation: 1440

Send URL parameters in Node,Express JS

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);
}

Upvotes: 2

Views: 4908

Answers (3)

Dreams
Dreams

Reputation: 6122

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.

Upvotes: 1

Conrad Lotz
Conrad Lotz

Reputation: 8818

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

Upvotes: 2

Gnanesh
Gnanesh

Reputation: 718

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
    }
});

Upvotes: 0

Related Questions