问题描述
我正在尝试通过POST请求发送用户名和密码来通过节点对站点进行身份验证,因为这就是登录表单所采用的方式.当使用邮递员进行尝试时,它可以正常工作,并且将我重定向到我的仪表板.
I'm trying to authenticate with a site through node by sending my username and password through a POST request, as that's how the login form seems to be doing it. When trying it out with Postman it works just fine and I'm redirected to the my dashboard.
不幸的是,用户名和密码字段未标记为用户名和密码,而是j_username和j_password.
Username and password fields are unfortunately not labeled as username and password, but as j_username and j_password.
这是邮递员显示给我的数据(供参考):
This is the data Postman shows me (for reference):
POST /path/for/auth/request HTTP/1.1
Host: site.com
Cache-Control: no-cache
Postman-Token: b6656210-caeb-2b62-d6b6-e10e642b200b
Content-Type: application/x-www-form-urlencoded
j_username=myusername&j_password=mypassword
所以我在节点上尝试一下:
So I try this in node:
var request = require('request');
request.post({
headers: {'content-type' : 'application/x-www-form-urlencoded'},
url: 'https://site.com/path/for/auth/request',
body: "j_username=myusername&j_password=mypassword"
}, function(err, res, body){
console.log(body);
});
但不幸的是,它恢复为空并且err设置为null.
But unfortunately it's coming back empty and err is set to null.
这里可能出什么问题了?还是有更好的方法来对此资源进行身份验证?据我所知,无法通过标头告诉基本身份验证.
What could be going wrong here? Or is there a better way of authenticating with this resource? As far as I can tell Basic Auth through a header isn't possible here.
谢谢.
推荐答案
该站点可能正在向您发送HTTP 302重定向,因此被认为是成功的,这就是为什么没有错误,但是也没有响应正文,这就是为什么它是空的".您需要查看响应中的statusCode以及 Location
标头.
The site is probably sending you an HTTP 302 redirect, so that's considered success, which is why there's no error, but there's also no response body, which is why it's "empty". You'll want to look at the statusCode as well as the Location
header in the response.
这篇关于通过Node.js中的POST请求进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!