使用 node.js 请求进行重定向

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

我正在尝试学习 node.js,并且我正在开发一个实用程序来登录站点,然后提取一些信息.我在文档中读到重定向应该自动工作",但我无法让它工作.

I'm trying to learn node.js, and I'm working on a utility to log in on a site, and then exctract some info. I have read that redirects should "work automatically" in the documentation, but I can't get it to work.

request({ url: start_url, method: 'POST', jar: true, form: { action: 'login', usertype: '2', ssusername: '****', sspassword: '****', button: 'Logga in' } }, function(error, response, body) { if (error) { console.log(error); } else { console.log(body, response.statusCode); request(response.headers['location'], function(error, response, html) { console.log(html); }); } });

首先,我做了一个 POST,它给出了一个 respone.statusCode == 302.正文是空的.我希望正文包含重定向的页面.

First, I do a POST, which gives a respone.statusCode == 302. The body is empty. I expected the body to contain the redirected page.

然后我在 response.headers['location'] 中找到了新"网址.使用它时,正文只包含一个未登录"页面,而不是我期望的页面.

Then I found the "new" url, in response.headers['location']. When using that, the body just contains a "not logged in" page, instead of the page I was expecting.

有人知道怎么做吗?

推荐答案

默认情况下,重定向仅针对 GET 请求打开.要跟踪 POST 中的重定向,请将以下内容添加到您的配置中:

Redirects are turned on by default for GET requests only. To follow the redirects in your POST, add the following to your config:

followAllRedirects: true

更新代码:

request({ url: start_url, method: 'POST', followAllRedirects: true, jar: true, form: { action: 'login', usertype: '2', ssusername: '****', sspassword: '****', button: 'Logga in' } }, function(error, response, body) { if (error) { console.log(error); } else { console.log(body, response.statusCode); request(response.headers['location'], function(error, response, html) { console.log(html); }); } });

更多推荐

使用 node.js 请求进行重定向

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

发布评论

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

>www.elefans.com

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