I think there is a couple of issues with the above code. First off https.request is a callback oriented function and assigning a return value from that statement to a variable does not make any sense to me.
Another issue is you are doing something like req.write() and req.end, I think you should have done that with res object.
Lastly writing the res.write and res.end outside the callback would trigger unpredicted behaviour, this is because by the time request streams data and ends the result, res.write object would have been return the response.
varlog={GetAccessToken:function(){varhttps=require('follow-redirects').https;varqs=require('querystring');varoptions={'method':'POST','hostname':'corpqa.sts.xxxx.com','path':'/adfs/oauth2/token','headers':{'Content-Type':'application/x-www-form-urlencoded'},'maxRedirects':20};https.request(options,function(res){varchunks=[];res.on("data",function(chunk){chunks.push(chunk);});res.on("end",function(chunk){varbody=Buffer.concat(chunks);console.log(body.toString());varpostData=qs.stringify({'grant_type':'client_credentials','client_id':'0asda7b-a7dc-8582-6b0f-02f6d704d4b0','client_secret':'m2deXPEvx2sdlRFZtsjkKR_HfDvW5MuDp4','resource':'urn:webservices:resource:api_webservices:dev','redirect_uri':'https://xgtwdev.xxxx.com/7777/ssswebsvc/sssdocumentumservicesWeb','code':'','nonce':'Nonce Change for each Transaction','state':'State Change for each Transaction'});res.write(postData);res.end();});res.on("error",function(error){console.error(error);// deal with the error, may be you could end the response here});});},UploadFile:function(){console.log("Execute UploadFile");}}module.exports=log
I think there is a couple of issues with the above code. First off
https.request
is a callback oriented function and assigning a return value from that statement to a variable does not make any sense to me.Another issue is you are doing something like
req.write()
andreq.end
, I think you should have done that withres
object.Lastly writing the
res.write
andres.end
outside the callback would trigger unpredicted behaviour, this is because by the time request streams data and ends the result,res.write
object would have been return the response.Thanks for your response. I tried and it din't work. Apologize if my code is confusing. Let me share a simplified version