我四处搜索,最终没有找到有关 bluebird 的 promisified 请求的 putAsync 方法的信息。
var request = require('request');
var Promise = require('bluebird');
Promise.promisifyAll(require("request"));
request.putAsync({
uri: buApiUrl,
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
name: BU,
workstations: formattedWorkStaions[BU]
})
}).spread(function (response, body) {
debugHelper.log(body);
}).catch(function (err) {
debugHelper.error(err);
});
上面是我的程序中的代码片段。并且它不发送 put 请求。使用postAsync时,如果会成功发送post请求。
谁能帮忙解释一下为什么吗?
最佳答案
我已经在 putAsync 代码片段中找到了错误的部分。我应该使用 json 而不是 body 作为有效负载的 key 。并且有效负载不需要被字符串化。 这是经过验证有效的新代码片段。
var request = require('request');
var Promise = require('bluebird');
Promise.promisifyAll(require("request"));
request.putAsync({
uri: buApiUrl,
headers: {
'content-type': 'application/json'
},
json: {
name: BU,
workstations: formattedWorkStaions[BU]
}
}).spread(function (response, body) {
debugHelper.log(body);
}).catch(function (err) {
debugHelper.error(err);
});
这非常棘手,导致了我的第二个问题。 除了方法类型之外,为什么 post 和 put 之间存在如此大的差异?
关于node.js - 如何在 Promisify 请求模块后正确使用 putAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34058305/