简体   繁体   English

通过Node.js中的POST请求进行身份验证

[英]Authenticate via POST request in Node.js

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. 我正在尝试通过POST请求发送用户名和密码来通过节点对站点进行身份验证,因为这就是登录表单似乎正在执行的操作。 When trying it out with Postman it works just fine and I'm redirected to the my dashboard. 当使用Postman尝试时,它可以正常工作,并且将我重定向到我的仪表板。

Username and password fields are unfortunately not labeled as username and password, but as j_username and j_password. 不幸的是,用户名和密码字段未标记为用户名和密码,而是标记为j_username和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);
});

But unfortunately it's coming back empty and err is set to null. 但不幸的是,它返回为空,并且err设置为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. 据我所知,无法通过标头告诉基本身份验证。

Thanks. 谢谢。

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". 该站点可能正在向您发送HTTP 302重定向,因此这被认为是成功的,这就是为什么没有错误,但是也没有响应正文,这就是为什么它为“空”的原因。 You'll want to look at the statusCode as well as the Location header in the response. 您需要查看响应中的statusCode以及Location标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM