HTTPS 请求 returns getaddrinfo ENOTFOUND
HTTPS Request returns getaddrinfo ENOTFOUND
我正在尝试向 https://petstore.swagger.io/v2/pets endpoint. Documentation can be found here: https://petstore.swagger.io/#/ 发出 HTTPS POST 请求。它使用 Postman 工作正常,但是当我使用 Node.js 使用相同的数据发出相同的请求时,我收到以下错误:
Error: getaddrinfo ENOTFOUND petstore.swagger.io/v2
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'petstore.swagger.io/v2'
}
完整代码如下:
import * as https from 'https';
async function main() {
const data = JSON.stringify({
name: 'doggie',
photoUrls: [
"string"
]
});
const options = {
hostname: "petstore.swagger.io/v2",
path: "/pet",
body: data
}
try {
let response = await httpRequest(options);
console.log(response);
} catch (err) {
console.log(err);
}
}
function httpRequest(options) {
return new Promise((resolve, reject) => {
const clientRequest = https.request(options, incomingMessage => {
// Response object.
let response = {
statusCode: incomingMessage.statusCode,
headers: incomingMessage.headers,
body: []
};
// Collect response body data.
incomingMessage.on('data', chunk => {
response.body.push(chunk);
});
// Resolve on end.
incomingMessage.on('end', () => {
if (response.body.length) {
response.body = response.body.join();
try {
response.body = JSON.parse(response.body);
} catch (error) {
// Silently fail if response is not JSON.
}
}
resolve(response);
});
});
// Reject on request error.
clientRequest.on('error', error => {
reject(error);
});
// Write request body if present.
if (options.body) {
clientRequest.write(options.body);
}
// Close HTTP connection.
clientRequest.end();
});
}
main();
我搞砸了什么?谢谢!
主机名只包含域,不包含路径的其余部分。您还需要提供 HTTP 方法和内容类型。
const options = {
hostname: "petstore.swagger.io",
path: "/v2/pet",
body: data,
method: "POST",
headers: {
'Content-Type': 'application/json'
}
}
我正在尝试向 https://petstore.swagger.io/v2/pets endpoint. Documentation can be found here: https://petstore.swagger.io/#/ 发出 HTTPS POST 请求。它使用 Postman 工作正常,但是当我使用 Node.js 使用相同的数据发出相同的请求时,我收到以下错误:
Error: getaddrinfo ENOTFOUND petstore.swagger.io/v2
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26) {
errno: -3008,
code: 'ENOTFOUND',
syscall: 'getaddrinfo',
hostname: 'petstore.swagger.io/v2'
}
完整代码如下:
import * as https from 'https';
async function main() {
const data = JSON.stringify({
name: 'doggie',
photoUrls: [
"string"
]
});
const options = {
hostname: "petstore.swagger.io/v2",
path: "/pet",
body: data
}
try {
let response = await httpRequest(options);
console.log(response);
} catch (err) {
console.log(err);
}
}
function httpRequest(options) {
return new Promise((resolve, reject) => {
const clientRequest = https.request(options, incomingMessage => {
// Response object.
let response = {
statusCode: incomingMessage.statusCode,
headers: incomingMessage.headers,
body: []
};
// Collect response body data.
incomingMessage.on('data', chunk => {
response.body.push(chunk);
});
// Resolve on end.
incomingMessage.on('end', () => {
if (response.body.length) {
response.body = response.body.join();
try {
response.body = JSON.parse(response.body);
} catch (error) {
// Silently fail if response is not JSON.
}
}
resolve(response);
});
});
// Reject on request error.
clientRequest.on('error', error => {
reject(error);
});
// Write request body if present.
if (options.body) {
clientRequest.write(options.body);
}
// Close HTTP connection.
clientRequest.end();
});
}
main();
我搞砸了什么?谢谢!
主机名只包含域,不包含路径的其余部分。您还需要提供 HTTP 方法和内容类型。
const options = {
hostname: "petstore.swagger.io",
path: "/v2/pet",
body: data,
method: "POST",
headers: {
'Content-Type': 'application/json'
}
}