当前,我正在使用REST API,该API需要顺序调用以设置身份验证。
//设置用户
//获取身份验证
http://localhost:8081/password密码
上述顺序调用在Browser和Postman上运行良好。但是,当我尝试使用axios.get()从Node JS调用它时,服务器将这2个请求视为完全不同的请求,并抛出诸如“错误的身份验证序列”之类的错误。
难道我们需要以某种方式明确提及标头来表示来自同一客户端的请求吗?如果是,请对此提供帮助。
app.get('/', (req, res) => {
let api = `${Url}/user name ${username}`;
console.log('api - ' + api);
//Set Username
axios
.get(api)
.then(response => {
console.log(response);
response.data.includes('USER SET') ? setPassword() : console.log(res.data);
})
.catch(err => console.log(err));
res.send('Hello Word');
});
setPassword = () => {
let api = `${Url}/password ${password}`;
console.log('api - ' + api);
axios
.get(api)
.then(res => {
console.log('success: ' + res.data);
})
.catch(err => console.log('SetPasswordError' + err));
};
答案 0 :(得分:0)
您要为其编写代码的应用程序实际上不是RESTful API,并且希望您保持连接打开以应对多个请求。