将 cURL 请求转换为 Node JS https 请求
Converting a cURL request to Node JS https request
我正在运行从终端发出以下 cURL 请求,它有效。
curl 'https://api.bitclout.com/get-single-profile' \
-H 'Connection: keep-alive' \
-H 'Accept: application/json, text/plain, /' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Ontel Mac OS X 11_2_3) AppleWebKit/537/36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36' \
-H 'Content-Type: application/json' \
-H 'Accept-Language: en, zh-TW;q=0.8,en-US;q=0.7' \
--data-raw '{"PublicKeyBase58Check":"","Username":"elonmusk"}' \
--compressed \
--insecure
现在我正在尝试将此代码转换为 Node JS javascript https 请求。我转换后的代码如下:
const http = require('https');
const options = {
method: 'POST',
hostname: 'api.bitclout.com',
port: null,
path: 'get-single-profile',
headers: {
'content-type': 'application/json',
"PublicKeyBase58Check": 'BC1YLhKJZZcPB2WbZSSekFF19UshsmmPoEjtEqrYakzusLmL25xxAJv',
"Username":"elonmusk"
}
};
const req = http.request(options, res => {
const chunks = []
res.on('data', chunk => {
chunks.push(chunk)
})
res.on('end', () => {
const body = Buffer.concat(chunks)
console.log(body.toString())
})
});
但是,当我 运行 上述代码时,出现以下错误:
events.js:292
throw er; // Unhandled 'error' event
^
Error: socket hang up
at connResetException (internal/errors.js:607:14)
at TLSSocket.socketOnEnd (_http_client.js:493:23)
at TLSSocket.emit (events.js:327:22)
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) Emitted 'error' event on
ClientRequest instance at:
at TLSSocket.socketOnEnd (_http_client.js:493:9)
at TLSSocket.emit (events.js:327:22)
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) { code: 'ECONNRESET' }
谁能告诉我 javascript 中的代码是什么以获得与 cURL 请求相同的输出?
这应该可以正常工作。
var request = require('request');
var headers = {
'Connection': 'keep-alive',
'Accept': 'application/json, text/plain, /',
'User-Agent': 'Mozilla/5.0 (Macintosh; Ontel Mac OS X 11_2_3) AppleWebKit/537/36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
'Content-Type': 'application/json',
'Accept-Language': 'en, zh-TW;q=0.8,en-US;q=0.7'
};
var dataString = '{"PublicKeyBase58Check":"","Username":"elonmusk"}';
var options = {
url: 'https://api.bitclout.com/get-single-profile',
method: 'POST',
headers: headers,
body: dataString
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);
我正在运行从终端发出以下 cURL 请求,它有效。
curl 'https://api.bitclout.com/get-single-profile' \
-H 'Connection: keep-alive' \
-H 'Accept: application/json, text/plain, /' \
-H 'User-Agent: Mozilla/5.0 (Macintosh; Ontel Mac OS X 11_2_3) AppleWebKit/537/36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36' \
-H 'Content-Type: application/json' \
-H 'Accept-Language: en, zh-TW;q=0.8,en-US;q=0.7' \
--data-raw '{"PublicKeyBase58Check":"","Username":"elonmusk"}' \
--compressed \
--insecure
现在我正在尝试将此代码转换为 Node JS javascript https 请求。我转换后的代码如下:
const http = require('https');
const options = {
method: 'POST',
hostname: 'api.bitclout.com',
port: null,
path: 'get-single-profile',
headers: {
'content-type': 'application/json',
"PublicKeyBase58Check": 'BC1YLhKJZZcPB2WbZSSekFF19UshsmmPoEjtEqrYakzusLmL25xxAJv',
"Username":"elonmusk"
}
};
const req = http.request(options, res => {
const chunks = []
res.on('data', chunk => {
chunks.push(chunk)
})
res.on('end', () => {
const body = Buffer.concat(chunks)
console.log(body.toString())
})
});
但是,当我 运行 上述代码时,出现以下错误:
events.js:292 throw er; // Unhandled 'error' event ^
Error: socket hang up at connResetException (internal/errors.js:607:14) at TLSSocket.socketOnEnd (_http_client.js:493:23) at TLSSocket.emit (events.js:327:22) at endReadableNT (internal/streams/readable.js:1327:12) at processTicksAndRejections (internal/process/task_queues.js:80:21) Emitted 'error' event on ClientRequest instance at: at TLSSocket.socketOnEnd (_http_client.js:493:9) at TLSSocket.emit (events.js:327:22) at endReadableNT (internal/streams/readable.js:1327:12) at processTicksAndRejections (internal/process/task_queues.js:80:21) { code: 'ECONNRESET' }
谁能告诉我 javascript 中的代码是什么以获得与 cURL 请求相同的输出?
这应该可以正常工作。
var request = require('request');
var headers = {
'Connection': 'keep-alive',
'Accept': 'application/json, text/plain, /',
'User-Agent': 'Mozilla/5.0 (Macintosh; Ontel Mac OS X 11_2_3) AppleWebKit/537/36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
'Content-Type': 'application/json',
'Accept-Language': 'en, zh-TW;q=0.8,en-US;q=0.7'
};
var dataString = '{"PublicKeyBase58Check":"","Username":"elonmusk"}';
var options = {
url: 'https://api.bitclout.com/get-single-profile',
method: 'POST',
headers: headers,
body: dataString
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);