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);
}
7
  • 1
    What's the error? Always post the error Commented May 31, 2017 at 6:28
  • anything related with this ? stackoverflow.com/questions/20089582/… Commented May 31, 2017 at 6:31
  • This gives me an internal server error with not much description. I am not sure with the way I set my URL parameter set to true. Commented May 31, 2017 at 6:32
  • 1
    Post the server logs. "due to some reason" is not helping at all. Commented May 31, 2017 at 6:50
  • so you want to make a post request to /thisway?variable=true ? Commented May 31, 2017 at 7:48

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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
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.

Comments

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

1 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.