'How to migrate from request to axios
I am using [https://github.com/huobiapi/REST-Node.js-demo][1] for my connection to huobi cryptocoin exchange but i am having some errors like :
(node:29550) Warning: a promise was rejected with a non-error: [object Undefined]
and
GET /v1/account/accounts/15548622/balance
{ Error: ESOCKETTIMEDOUT
at ClientRequest.<anonymous> (/home/ttr/node_modules/request/request.js:816:19)
at Object.onceWrapper (events.js:285:13)
at ClientRequest.emit (events.js:197:13)
at TLSSocket.emitRequestTimeout (_http_client.js:668:40)
at Object.onceWrapper (events.js:285:13)
at TLSSocket.emit (events.js:197:13)
at TLSSocket.Socket._onTimeout (net.js:447:8)
at listOnTimeout (timers.js:324:15)
at processTimers (timers.js:268:5) code: 'ESOCKETTIMEDOUT', connect: false }
I believe they are related to file:
framework/httpClient.js
which has an outdated module "request" can be changed to axios module so my errors may dissappear.
const http = require('http');
const request = require('request');
const moment = require('moment');
const logger = console;
var default_post_headers = {
'content-type': 'application/json;charset=utf-8',
}
var agentOptions = {
keepAlive: true,
maxSockets: 256,
}
exports.get = function(url, options) {
return new Promise((resolve, reject) => {
options = options || {};
var httpOptions = {
url: url,
method: 'get',
timeout: options.timeout || 3000,
headers: options.headers || default_post_headers,
proxy: options.proxy || '',
agentOptions: agentOptions
}
request.get(httpOptions, function(err, res, body) {
if (err) {
reject(err);
} else {
if (res.statusCode == 200) {
resolve(body);
} else {
reject(res.statusCode);
}
}
}).on('error', logger.error);
});
}
exports.post = function(url, postdata, options) {
return new Promise((resolve, reject) => {
options = options || {};
var httpOptions = {
url: url,
body: JSON.stringify(postdata),
method: 'post',
timeout: options.timeout || 3000,
headers: options.headers || default_post_headers,
proxy: options.proxy || '',
agentOptions: agentOptions
};
request(httpOptions, function(err, res, body) {
if (err) {
reject(err);
} else {
if (res.statusCode == 200) {
resolve(body);
} else {
reject(res.statusCode);
}
}
}).on('error', logger.error);
});
};
exports.form_post = function(url, postdata, options) {
return new Promise((resolve, reject) => {
options = options || {};
var httpOptions = {
url: url,
form: postdata,
method: 'post',
timeout: options.timeout || 3000,
headers: options.headers || default_post_headers,
proxy: options.proxy || '',
agentOptions: agentOptions
};
request(httpOptions, function(err, res, body) {
if (err) {
reject(err);
} else {
if (res.statusCode == 200) {
resolve(body);
} else {
reject(res.statusCode);
}
}
}).on('error', logger.error);
});
};
So far i tried :
return new Promise((resolve, reject) => {
options = options || {};
var httpOptions = {
url: url,
method: 'get',
timeout: options.timeout || 3000,
headers: options.headers || default_post_headers,
proxy: options.proxy || '',
agentOptions: agentOptions
}
axios.get(httpOptions, function(err, res, body) {
if (err) {
reject(err);
} else {
if (res.statusCode == 200) {
resolve(body);
} else {
reject(res.statusCode);
}
}
}).on('error', logger.error);
});
}
Solution like this but seems like they dont work exactly same so not worked.
[1]: https://github.com/huobiapi/REST-Node.js-demo
Solution 1:[1]
I just migrated from request to axios, and i recommand the following changes:
import qs package
const httpsAgent = new https.Agent({
rejectUnauthorized: false / true,
cert: <your cert>
key: <your key> })
var httpOptions = {
url: url
data: qs.stringify(postdata)
method: 'post',
timeout: options.timeout || 3000,
headers: options.headers || default_post_headers,
proxy: options.proxy || '', (not sure about this)
httpsAgent };
And this is the right way to send the request:
axios(httpOptions).then((resp)=>{
// on success - for exmaple: console.log(response.status);
}).catch((err) => {
//on failure - for example: return null;
});
your request on the one hand contains method:post and on the other hand you did axios.get if your request payload contains method field, you can just run axios(options)
Solution 2:[2]
i just removed bluebird from modules and errors gone.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | trmt |