当我用 mocha 调用 api 时出现 401 错误
getting 401 error when i call the api with mocha
我是 mocha 的新手,现在我正在用 mocha 进行 api 调用,但是我收到了这个错误 Uncaught AssertionError: expected { Object (domain, _events, ...) } to have status code 200 but got 401
,谁能帮我解释为什么我会收到这个错误?这是我的完整代码
it('Get Organizations', function(done) {
let current_token = currentResponse['token'];
let headers = [{"content-type":"application/json","vrc-access-token": current_token }];
console.log(headers);
chai.request(app)
.post('entities')
.set(headers)
.send({"key":"organizations","operation":"findAll"})
.end(function (err, res) {
currentResponse = res.body;
expect(err).to.be.null;
expect(res).to.have.status(200);
done();
});
});
只是在docs的基础上,好像chai-http不支持这样设置headers,需要单独添加即
chai.request(app)
.post('entities')
.set('Content-Type', 'application/json')
.set('vrc-access-token', current_token)
.send({"key":"organizations","operation":"findAll"})
.end(function (err, res) {
currentResponse = res.body;
expect(err).to.be.null;
expect(res).to.have.status(200);
done();
});
我是 mocha 的新手,现在我正在用 mocha 进行 api 调用,但是我收到了这个错误 Uncaught AssertionError: expected { Object (domain, _events, ...) } to have status code 200 but got 401
,谁能帮我解释为什么我会收到这个错误?这是我的完整代码
it('Get Organizations', function(done) {
let current_token = currentResponse['token'];
let headers = [{"content-type":"application/json","vrc-access-token": current_token }];
console.log(headers);
chai.request(app)
.post('entities')
.set(headers)
.send({"key":"organizations","operation":"findAll"})
.end(function (err, res) {
currentResponse = res.body;
expect(err).to.be.null;
expect(res).to.have.status(200);
done();
});
});
只是在docs的基础上,好像chai-http不支持这样设置headers,需要单独添加即
chai.request(app)
.post('entities')
.set('Content-Type', 'application/json')
.set('vrc-access-token', current_token)
.send({"key":"organizations","operation":"findAll"})
.end(function (err, res) {
currentResponse = res.body;
expect(err).to.be.null;
expect(res).to.have.status(200);
done();
});