尝试在 node.js 中使用 axios 复制 curl -c post/get
Trying to repliate curl -c post/get with axios in node.js
做一些 Ethereum/Chainlink 工作并尝试从我的 Chainlink 节点上的 API 端点提取一些 gas 价格信息。与 curl 一起工作,但我在 node.js.
中使用 axios 时遇到 cookie 问题
首先我POST获取cookie
curl -c cookiefile \
-d '{"email":"user@example.com", "password":"password"}' \
-X POST -H 'Content-Type: application/json' \
http://localhost:6688/sessions
然后像这样获取数据:
curl -b cookiefile http://localhost:6688/v2/config
这是我的 axios 代码,应该是等效的。尝试 POST,存储 cookie,然后 GET,但 GET 验证失败。 POST 似乎 return 请求的“set-cookie”字段没问题。
const axios = require('axios').default;
const instance = axios.create({
baseURL: 'http://localhost:6688/v2/config',
timeout: 1000,
headers: {'Content-Type': 'application/json'}
});
_getGasPrice()
function _getGasPrice() {
axios.post('http://localhost:6688/sessions', {
email: 'email',
password: 'pass'
})
.then(function (response) {
const cookie = response.headers["set-cookie"];
console.log(cookie);
instance.defaults.headers.Cookie = cookie;
axios.get('http://localhost:6688/v2/config'}
.then(function (repsonse) {
console.log(response.data.attributes.ethGasPriceDefault);
})
.catch((err) => {
console.log(err);
})
})
.catch((err) => {
console.log(err);
})
}
感谢任何帮助,谢谢。
const axios = require('axios').default;
_getGasPrice()
function _getGasPrice() {
axios.post('http://localhost:6688/sessions', {
email: 'email',
password: 'password',
headers: {'Content-Type': 'application/json'}
})
.then(function (response) {
const cookie = response.headers["set-cookie"];
console.log(response);
axios.get('http://localhost:6688/v2/config', {headers: {Cookie: cookie}})
.then(function (response) {
let data = Object.keys(response.data).map(i => response.data[i])
console.log(data[0].attributes.ethGasPriceDefault);
})
.catch((err) => {
console.log(err);
})
})
.catch((err) => {
console.log(err);
})
}
这最终成功了,保存了 cookie,然后将其传递到 GET 请求的 header 中。此外,GET 响应的格式是 [object] 而不是 JSON,我需要重新映射它以获得我想要的属性。
做一些 Ethereum/Chainlink 工作并尝试从我的 Chainlink 节点上的 API 端点提取一些 gas 价格信息。与 curl 一起工作,但我在 node.js.
中使用 axios 时遇到 cookie 问题首先我POST获取cookie
curl -c cookiefile \
-d '{"email":"user@example.com", "password":"password"}' \
-X POST -H 'Content-Type: application/json' \
http://localhost:6688/sessions
然后像这样获取数据:
curl -b cookiefile http://localhost:6688/v2/config
这是我的 axios 代码,应该是等效的。尝试 POST,存储 cookie,然后 GET,但 GET 验证失败。 POST 似乎 return 请求的“set-cookie”字段没问题。
const axios = require('axios').default;
const instance = axios.create({
baseURL: 'http://localhost:6688/v2/config',
timeout: 1000,
headers: {'Content-Type': 'application/json'}
});
_getGasPrice()
function _getGasPrice() {
axios.post('http://localhost:6688/sessions', {
email: 'email',
password: 'pass'
})
.then(function (response) {
const cookie = response.headers["set-cookie"];
console.log(cookie);
instance.defaults.headers.Cookie = cookie;
axios.get('http://localhost:6688/v2/config'}
.then(function (repsonse) {
console.log(response.data.attributes.ethGasPriceDefault);
})
.catch((err) => {
console.log(err);
})
})
.catch((err) => {
console.log(err);
})
}
感谢任何帮助,谢谢。
const axios = require('axios').default;
_getGasPrice()
function _getGasPrice() {
axios.post('http://localhost:6688/sessions', {
email: 'email',
password: 'password',
headers: {'Content-Type': 'application/json'}
})
.then(function (response) {
const cookie = response.headers["set-cookie"];
console.log(response);
axios.get('http://localhost:6688/v2/config', {headers: {Cookie: cookie}})
.then(function (response) {
let data = Object.keys(response.data).map(i => response.data[i])
console.log(data[0].attributes.ethGasPriceDefault);
})
.catch((err) => {
console.log(err);
})
})
.catch((err) => {
console.log(err);
})
}
这最终成功了,保存了 cookie,然后将其传递到 GET 请求的 header 中。此外,GET 响应的格式是 [object] 而不是 JSON,我需要重新映射它以获得我想要的属性。