[英]Call .post() API-Easy
我正在使用API-easy,如何在_id_user中獲得此結果並將調用發送給.post(),謝謝。 例如
var APIeasy = require('api-easy'),
assert = require('assert');
var _id_user;
var suite = APIeasy.describe('Test User');
suite.use('localhost', 3000)
.discuss('Test')
.setHeader('Content-Type', 'application/x-www-form-urlencoded')
.post('/user/authenticate', {data: '{"email":"emailuser@email.com","password":"123456"}')
.expect('should respond with ID user', function (err, res, body) {
_id_user = body; // I need this result to be sent in the next call .post()
}).next()
.post('/user/validate',{ data : _id_user}) // this result always comes null
.expect('should respond TRUE', function (_err, _res, _body) {
}).export(module);
解決此問題的正確方法是使用before()調用來修改post參數。 您可以直接修改“發送”請求的內容。
var APIeasy = require('api-easy'),
assert = require('assert');
var suite = APIeasy.describe('Test User');
suite.use('localhost', 3000)
.discuss('Test')
.setHeader('Content-Type', 'application/x-www-form-urlencoded')
.post('/user/authenticate', {data: '{"email":"emailuser@email.com","password":"123456"}')
.expect('should respond with ID user', function (err, res, body) {
suite.before('setUserId', function(outgoing) {
//use outgoing.body for post requests and outgoing.uri for get requests
outgoing.body = outgoing.body.replace('_ID_USER',body);
return outgoing;
});
}).next()
.post('/user/validate',{ data : '_ID_USER'})
.expect('should respond TRUE', function (_err, _res, _body) {
//you can unbefore() here if you need it
suite.unbefore('setUserId');
}).export(module);
那是由於回調函數的異步特性。 在回調函數中做第二個帖子,即在_id_user=body;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.