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

编程入门 行业动态 更新时间:2024-10-25 16:23:14
本文介绍了通过Node.js中的POST请求进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试通过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 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: 'site/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请求进行身份验证

本文发布于:2023-10-10 01:23:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1477304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:身份验证   Node   js   POST

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!