Node.js NodeJS从请求切换到axios:未传递凭据
到目前为止,我一直在使用请求承诺,但现在已被弃用。我选择了axios作为继任者,并修改了代码,但不幸的是,我一直收到错误401“未经授权”。axios似乎没有传递给定的身份验证信息。我的旧源代码如下所示:Node.js NodeJS从请求切换到axios:未传递凭据,node.js,request,axios,Node.js,Request,Axios,到目前为止,我一直在使用请求承诺,但现在已被弃用。我选择了axios作为继任者,并修改了代码,但不幸的是,我一直收到错误401“未经授权”。axios似乎没有传递给定的身份验证信息。我的旧源代码如下所示: let options = { method: 'GET', uri: 'https://example.com/bla', auth: { user: 'USER', pass: 'PASSWORD', sendImmediately: true },
let options = {
method: 'GET',
uri: 'https://example.com/bla',
auth: {
user: 'USER',
pass: 'PASSWORD',
sendImmediately: true
},
headers: {
'X-CSRF-Token': 'Fetch'
},
resolveWithFullResponse: true
};
let rpWithDefaults = rp.defaults({
rejectUnauthorized: false,
jar: true
});
let response = await rpWithDefaults(options);
const axios = require('axios').default;
const https = require('https');
const response = await axios({
method: 'GET',
url: 'https://example.com/bla',
headers: {
'X-CSRF-Token': 'Fetch'
},
httpsAgent: new https.Agent({
rejectUnauthorized: false,
auth: {
user: 'USER',
pass: 'PASSWORD'
}
}),
resolveWithFullResponse: true
});
调整后的代码段如下所示:
let options = {
method: 'GET',
uri: 'https://example.com/bla',
auth: {
user: 'USER',
pass: 'PASSWORD',
sendImmediately: true
},
headers: {
'X-CSRF-Token': 'Fetch'
},
resolveWithFullResponse: true
};
let rpWithDefaults = rp.defaults({
rejectUnauthorized: false,
jar: true
});
let response = await rpWithDefaults(options);
const axios = require('axios').default;
const https = require('https');
const response = await axios({
method: 'GET',
url: 'https://example.com/bla',
headers: {
'X-CSRF-Token': 'Fetch'
},
httpsAgent: new https.Agent({
rejectUnauthorized: false,
auth: {
user: 'USER',
pass: 'PASSWORD'
}
}),
resolveWithFullResponse: true
});
我做错了什么
谢谢我以前没有使用过
httpsAgent
参数,但我认为在Axios中进行https调用不需要它。以下内容对你有用吗
const axios = require('axios').default;
const response = await axios({
method: 'GET',
baseURL: 'https://example.com/',
url: '/bla',
headers: {
'X-CSRF-Token': 'Fetch'
},
auth: {
user: 'USER',
pass: 'PASSWORD'
}
});
可能相关:我以前没有使用过
httpsAgent
参数,但我认为在Axios中进行https调用不需要它。以下内容对你有用吗
const axios = require('axios').default;
const response = await axios({
method: 'GET',
baseURL: 'https://example.com/',
url: '/bla',
headers: {
'X-CSRF-Token': 'Fetch'
},
auth: {
user: 'USER',
pass: 'PASSWORD'
}
});
可能相关:Axios doc说您需要通过这条路
// `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
// This will set an `Authorization` header, overwriting any existing
// `Authorization` custom headers you have set using `headers`.
// Please note that only HTTP Basic auth is configurable through this parameter.
// For Bearer tokens and such, use `Authorization` custom headers instead.
auth: {
username: 'janedoe',
password: 's00pers3cret'
},
那么,您是否尝试将密钥从user
和pass
更改为username
和password
const axios = require('axios').default;
const https = require('https');
const response = await axios({
method: 'GET',
url: 'https://example.com/bla',
headers: {
'X-CSRF-Token': 'Fetch'
},
auth: {
username: '<user-name>',
password: '<password>'
},
resolveWithFullResponse: true
});
const axios=require('axios')。默认值;
常量https=require('https');
常数响应=等待axios({
方法:“GET”,
网址:'https://example.com/bla',
标题:{
'X-CSRF-Token':'Fetch'
},
认证:{
用户名:“”,
密码:“”
},
resolveWithFullResponse:true
});
Axios doc说您需要从这边通过
// `auth` indicates that HTTP Basic auth should be used, and supplies credentials.
// This will set an `Authorization` header, overwriting any existing
// `Authorization` custom headers you have set using `headers`.
// Please note that only HTTP Basic auth is configurable through this parameter.
// For Bearer tokens and such, use `Authorization` custom headers instead.
auth: {
username: 'janedoe',
password: 's00pers3cret'
},
那么,您是否尝试将密钥从user
和pass
更改为username
和password
const axios = require('axios').default;
const https = require('https');
const response = await axios({
method: 'GET',
url: 'https://example.com/bla',
headers: {
'X-CSRF-Token': 'Fetch'
},
auth: {
username: '<user-name>',
password: '<password>'
},
resolveWithFullResponse: true
});
const axios=require('axios')。默认值;
常量https=require('https');
常数响应=等待axios({
方法:“GET”,
网址:'https://example.com/bla',
标题:{
'X-CSRF-Token':'Fetch'
},
认证:{
用户名:“”,
密码:“”
},
resolveWithFullResponse:true
});
Hell,我读了这篇文档并检查了属性名称“auth”,但我没有识别下面的属性名称。现在可以了。谢谢!它发生了,酷!见鬼,我看了这份文件,检查了财产名称“auth”,但我没有认出下面的财产名称。现在可以了。谢谢!它发生了,酷!