使用 promise 在 nodejs 中发出 http put 请求
Make http put request in nodejs with promise
我需要在 alexa skill 中向外部 api 发出 put 请求,但我没有找到该怎么做,请帮助我,你会幸运 3 分钟
我需要在 node.js
中使用 https 发送带有正文的放置请求
const http = require("https");
const getRequest = function(url) {
return new Promise((resolve, reject) => {
const client = url.startsWith("https") ? require("https") : require("http");
const request = client.get(url, response => {
if (response.statusCode < 200 || response.statusCode >+ 300) {
reject(new Error("Failed with status code: " + response.statusCode));
}
const body = [];
response.on("data", chunk => body.push(chunk));
response.on("end", () => resolve(body.join("")));
});
request.on("error", err => reject(err));
});
};```
为什么不使用 request?仅 return 解决或拒绝取决于 api-结果
return new Promise((resolve, reject) =>{
request({
uri: <apiURL>,
method: 'PUT',
body: <DATA>,
json: true,
headers: {
'Content-type': 'application/json'
},
}, (error, response, body) => {
if(error){
return reject(error);
}
return resolve(body);
});
});
我需要在 alexa skill 中向外部 api 发出 put 请求,但我没有找到该怎么做,请帮助我,你会幸运 3 分钟
我需要在 node.js
中使用 https 发送带有正文的放置请求const http = require("https");
const getRequest = function(url) {
return new Promise((resolve, reject) => {
const client = url.startsWith("https") ? require("https") : require("http");
const request = client.get(url, response => {
if (response.statusCode < 200 || response.statusCode >+ 300) {
reject(new Error("Failed with status code: " + response.statusCode));
}
const body = [];
response.on("data", chunk => body.push(chunk));
response.on("end", () => resolve(body.join("")));
});
request.on("error", err => reject(err));
});
};```
为什么不使用 request?仅 return 解决或拒绝取决于 api-结果
return new Promise((resolve, reject) =>{
request({
uri: <apiURL>,
method: 'PUT',
body: <DATA>,
json: true,
headers: {
'Content-type': 'application/json'
},
}, (error, response, body) => {
if(error){
return reject(error);
}
return resolve(body);
});
});