1

I already have a request object. Now all i have to do is change the host and make http request again.
//URL type : www.something.com/a/b?w=2&e=2

fun(req,res){

 options = {
   host : <newhost>
   method : req.method
   path : req.path
   headers : req.headers
  }
  http.request(options,...)
}

Now how do i send query string(w=2&e=2) in this option.
I can do it using request module(in nodejs) but that follows redirect(HTTP 302) as well.

Thanks,
Shantanu

2
  • 1
    You can tell request to not follow redirects. followRedirect: false Commented Oct 1, 2014 at 22:38
  • that worked..thanks a lot..can u post this as answer. Commented Oct 1, 2014 at 22:50

2 Answers 2

2

http can do this as well

var queryString = 'w=2&e=2';
options = {
   host : <newhost>
   method : req.method
   path : req.path + '?' + queryString // say, var queryString = 'w=2&e=2'  
   headers : req.headers
  }
  http.request(options,...)
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to not work, I have request that send query string with github token and if I use browser I got the response but in node I get rate limit error.
2

A better way is to stringify an object with nodes querystring module. This way you can pass an options object to a custom queryBuilder function and reuse it for multiple requests of varying values. Here is a basic example.

    var querystring = require('querystring');
    var http = require('http');

    var options = {
        host : 'www.host.com',
        path : ''
    }

    var queryBuilder = function (object, callback) {
        callback(querystring.stringify(object));
    };

    queryBuilder({ w: '2', e: '2' }, callback(data){
        options.path = data;
        http.request(options, function (req, res) {
            //do something with the response
        }).end();
    });

this will set the path equal to w=2&e=2

1 Comment

Thanks, a little typo: querysring should be querystring

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.