title: Update headers in a nodejs http.request
body: <p>There is a way in nodejs to modify response headers  when calling a second nested http.request that return some data from another server ?
  I always get this error, from the below functions...</p>
  
  <pre><code> catch error: Error: Can't set headers after they are sent.
  </code></pre>
  
  <p>Main function from where I call to check if the token is valid:
       req.on('end', function (){<br>
              console.log('GET to /api, Do I have privileges with %s?', access_token);
              validadToken(req, res, next);
          });</p>
  
  <p>This is the funciton to validate the token against the OAuth2 server, if the respond is 401, I tried to obtain an new refresh_token:</p>
  
  <pre><code>    function validadToken (req, res, next ) {
          var options = { port: 8080, 
                          path: '/api', 
                          host: 'localhost', 
                          method: 'GET' , 
                          headers: {'Authorization': 'Bearer '+ access_token,
                                'Content-Type':'application/x-www-form-urlencoded'} };
          req2 = http.request(options, 
              function (response){
                  response.setEncoding('utf8');
                  response.on('data', function (chunk) {
                      console.log(".-response=&gt; ", chunk);
                      console.log("statusCode: " + response.statusCode);
  
                      if(response.statusCode == 401){
                          console.log();
                          console.log("===&gt;&gt;&gt;&gt; call to renew / wait for me");
                          renewToken(response, res, next); 
  
                      }else if(response.statusCode === 200){
                          console.log("Valid Token... we keep it going!");
                      }
                      next();             
                  });  
              });
          req2.write(formData);
  
          req2.end();         
  }
  </code></pre>
  
  <p>From here I try to get the refresh token, but when I try to update the new value in the headers  I get the above error. The value for refresh_token is recovery from a response from the previously function</p>
  
  <pre><code>    function renewToken (req, res){
      var userUrl = 'grant_type=refresh_token&amp;refresh_token='+refresh_token+'&amp;client_id=XXX&amp;client_secret=XXX';
      var optRenew = {    port: 8080, 
                          path: '/token', 
                          host: 'localhost', 
                          method: 'POST' , 
                          headers: {'Content-Type':'application/x-www-form-urlencoded'} };
  
      renew = http.request(optRenew, 
              function (responseRenew){
                console.log("responseRenew");
                responseRenew.setEncoding('utf8');
  
                responseRenew.on('data', function (chunk){
                          console.log(".-responseRenew=&gt; ", chunk);
                          console.log(".-responseRenew=&gt; ", responseRenew.statusCode);
                          accessTokenValue = getToken("access_token", chunk, ',', ':', '"');
                          refreshTokenValue = getToken("refresh_token", chunk, ',', ':', '"'); 
                          console.log("Headers Sent (renew) ? "+ res.headersSent);
                          console.log("¡¡¡New values!!!\n" + accessTokenValue +' '+ refreshTokenValue);     
  
                          res.setHeader("Set-Cookie", ["ninja="+accessTokenValue, "samurai="+refreshTokenValue]);
                          console.log("envio ....");
                        })
              });
      renew.write(userUrl);
      renew.end();  
  }
  </code></pre>