[英]how to make multiple requests using request module node.js?
i am using nodejs request module
. 我正在使用
nodejs request module
。 how to make multiple requests for a session after user login using this? 使用此用户登录后,如何发出多个会话请求? how to make sure that the user is logged in and form data is successfuly submitted using request module?
如何确保用户已登录并且使用请求模块成功提交了表单数据? i am trying the example in the docs to submit the form for user login but it always redirects on the default page.
我正在尝试文档中的示例来提交表单以供用户登录,但它始终在默认页面上重定向。 can anyone guide me through?
谁能指导我?
THANKS 谢谢
You need to save cookie after login request, use cookie in next request. 登录请求后,您需要保存cookie,在下一个请求中使用cookie。
var cookie = null
login()
function login() {
request(host)
.post('/rest/user/login')
.send({email: 'email@domain.com', password: 'qweqwe'})
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) throw err
if (!res.body.error) {
cookie = res.headers['set-cookie']
nextRequest()
}
})
}
function nextRequest() {
request(host)
.post('/rest/data')
.set('cookie', cookie)
.send({
data: 'data'
})
.expect('Content-Type', /json/)
.expect(200)
.end(function (err, res) {
if (err) throw err
console.log(res.body.data)
})
}
As your wish request example, this tested. 作为您的愿望示例,此测试已完成。 Here are simple use.
这是简单的用法。
var request = require('request');
var host = 'http://localhost'
var loginUrl = host + '/rest/user/login'
var nextUrl = host + '/rest/next/request'
var j = request.jar()
function login(jar, callback) {
request.post({url: loginUrl, jar: jar}, callback).form({username: 'user1', password: 'pass1'})
}
function post(jar, callback) {
request.post({url: nextUrl, jar: jar}, callback).form({id: 1})
}
login(j, function (err, response, body) {
//check err, response, body
post(j, function(err, response, body){
//check err, response, body
console.log(body)
})
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.