ios 凭据验证,如何使用axios发送基本身份验证

I'm trying to implement the following code, but something is not working. Here is the code:

var session_url = 'http://api_address/api/session_endpoint';

var username = 'user';

var password = 'password';

var credentials = btoa(username + ':' + password);

var basicAuth = 'Basic ' + credentials;

axios.post(session_url, {

headers: { 'Authorization': + basicAuth }

}).then(function(response) {

console.log('Authenticated');

}).catch(function(error) {

console.log('Error on Authentication');

});

It's returning a 401 error. When I do it with Postman there is an option to set Basic Auth; if I don't fill those fields it also returns 401, but if I do, the request is successful.

Any ideas what I'm doing wrong?

Here is part of the docs of the API of how to implement this:

This service uses Basic Authentication information in the header to establish a user session. Credentials are validated against the Server. Using this web-service will create a session with the user credentials passed and return a JSESSIONID. This JSESSIONID can be used in the subsequent requests to make web-service calls.*

解决方案

There is an "auth" parameter for Basic Auth:

auth: {

username: 'janedoe',

password: 's00pers3cret'

}

Example:

await axios.post(session_url, {}, {

auth: {

username: uname,

password: pass

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值