我是一个相当新的Node,并尝试了一些代码。我正在使用express,body-parser和request从POST返回一个响应到一个web API。我最终打算做的是,如果POST响应中的一个元素等于"OK“,则继续沿着特定路径进行操作。
然而,当我运行request时,它是实际的console.log而不是响应-我需要使用像Bluebird这样的东西来帮助做这件事吗?
const apipost = {
method: 'POST',
url: '***',
headers: {
'Accept': '*/*',
'Content-Type': 'application/json'
},
body: message,
};
function exists(error, response, body){
if(JSON.parse(body).Status == "OK"){
console.log('yipee');
return true;
} else {
console.log('problems');
return false;
}
};
var responsetest = request(apipost, exists);
console.log(responsetest);
发布于 2020-04-15 07:13:42
请求已被弃用,请使用node-fetch。
const fetch = require('node-fetch');
fetch('urlhere', {
method: 'POST',
headers: {
'Accept': '*/*',
'Content-Type': 'application/json'
},
body: message,
})
.then(res => res.json())
.then(json => {
if(json.Status == 'OK'){
console.log('yipee', json);
} else {
console.log('problems', json);
}
});https://stackoverflow.com/questions/61217770
复制相似问题