我想通过我的Frisby测试检查数据是否实际成功发布到我的后端。我可以在控制台中看到URL调用成功,响应代码为201.但是,当我打印req.body时,它是空的。
describe("POST /createSecret", function() {
it('returns a status code of 201', function(done) {
//Send a POST req. to create secret data
frisby.create('GET JSON data from an endpoint')
.post('http://localhost:3000/createSecret', {
'secret_name' : barbicanRequestData.secret_name,
'expiration' : barbicanRequestData.expiration,
'algorithm' : barbicanRequestData.algorithm,
'bit_length' : barbicanRequestData.bit_length,
'mode' : barbicanRequestData.mode,
'payload' : barbicanRequestData.payload,
'content_type' : barbicanRequestData.content_type,
'content_encoding' : barbicanRequestData.content_encoding,
'secret_type' : barbicanRequestData.secret_type
})
.expectStatus(201)
.expectHeader('Content-Type', 'application/json; charset=utf-8')
.expectJSON({ 'secret_ref': 'Secret was successfully created!' })
.toss();
done();
});
});
else if (req.method == "POST") {
console.log("Request is POST");
console.log(res.body);
//Test a POST response for creating a secret using the Barbican API
res.status(201).send({
'secret_ref': "Secret was successfully created!",
'Content-Type': 'application/json; charset=utf-8'
});
}
答案 0 :(得分:0)
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
这使我能够成功访问来自body的数据!