Https Post Request with body
Https Post Request with body
我需要从Microsoft AD获得令牌,为此我需要使用BODY
进行POST
请求。我有这个代码,返回一个缺失的grant_type
参数在体的错误。我猜它只是没有发送身体信息。如何配置请求发送正文?
任何帮助将不胜感激!
这是我的代码:
var options = {
hostname: 'login.microsoftonline.com',
path: '/common/oauth2/token',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data:{
grant_type: oauthData.grant_type,
client_id: oauthData.client_id,
code: oauthData.code,
redirect_uri: oauthData.redirect_uri,
resource: oauthData.resource
}
};
function requestToken(options){
var req = https.request(options, function(res) {
console.log("statusCode: ", res.statusCode);
console.log("headers: ", res.headers);
res.on('data', function(d) {
process.stdout.write(d);
});
});
req.end();
req.on('error', function(e) {
console.error('---- Error ----');
console.error(e);
});
}//end of requestToken()
它没有发送body,因为你没有告诉req对象。
- 从选项中删除data{}对象。
- 为请求添加request. write()
req.write(qs.stringify({
grant_type: 'authorization_code',
client_id: config.clientId,
scope: config.scope,
client_secret: config.clientSecret,
tenant: config.tenant
}))
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- WebComponentsJS with IE10
- 指令的模板必须只有一个根元素:With restrict E&替换true
- 不同页面背景的body标记上的角度ng样式
- contentWindow.document.body is null
- timeago.js with datatable and PHP
- Ajax and Json with Rails
- errors with Javascript try catch
- how to split a string with ','
- jQuery e.stopPropagation Conflict With body onclick
- JQuery appendTo Div with ID instead of appendTo('body
- AngularJS $http.post with body
- Firefox CSS transitions with body overflow:hidden
- Creating email link with dynamically generated body with htm
- Https Post Request with body
- Javascript: $('body:contains("")') with ht