如何在 Node.js 中模拟 POST 请求后访问响应主体?
How to access response Body after simulating a POST request in Node.js?
我已经尝试了很长时间了。
我想从包含成人内容的 subreddit 中删除内容。
但是,问题是,您必须先回答一个简单的问题,然后才能访问该页面,即您是否年满 18 岁。
我对源代码做了一些研究,发现解决方案是一个简单的 POST 请求。您需要在其中发送参数"over18=yes"。
但我的问题是,在 post.
之后我无法访问响应正文
这是在节点中使用 http 请求的代码。我什至尝试过使用节点 "request" 模块,但也没有任何帮助。
希望能在这里找到可以帮助我的人。
var http = require("http");
var options = {
host: 'www.reddit.com',
port: 80,
path: '/over18?dest=http%3A%2F%2Fwww.reddit.com%2Fr%2Fnsfw&over18=yes',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
这里是使用节点请求模块的代码
var request = require("request");
request.post({url:'http://www.reddit.com/over18?dest=http%3A%2F%2Fwww.reddit.com%2Fr%2Fnsfw', form: {over18:'yes'}}, function(err,httpResponse,body){
console.log(body);
});
我正在尝试访问的 URL 是 http://www.reddit.com/r/nsfw
简而言之,当您点击 YES 按钮时,表单将 over18=yes 参数发送到 url http://www.reddit.com/over18?dest=http%3A%2F%2Fwww.reddit.com%2Fr%2Fnsfw using POST method. Then, server responds with an 302 Redirection header, cookie with value over18=1 and finally redirects to url http://www.reddit.com/r/nsfw 使用 GET 请求。然后,服务器只检查您是否有具有所需值的 cookie。
您只需要使用 GET 方法使用 cookie 直接向最终 url 请求。
var request = require("request");
var target = "http://www.reddit.com/r/nsfw";
var jar = request.jar();
var cookie = request.cookie("over18=1");
cookie.domain = "reddit.com";
cookie.path = "/";
jar.setCookie(cookie, target, function(error, cookie) {
console.log(error);
console.log(cookie);
});
request({
uri: target,
method: "GET",
jar: jar
}, function(error, response, body) {
console.log(response.statusCode);
console.log(body);
});
我也 运行 在 ahem 做一些研究的时候加入了这个。这是我的版本:
var url = 'http://www.reddit.com/r/nsfw/';
var request = require('request');
request = request.defaults({jar: true });
request.post({
followAllRedirects: true,
url: 'http://www.reddit.com/over18?dest=' + encodeURIComponent(url),
form: {uh: '', over18: 'yes', }
}, function(err, httpResponse, html) {
…
});
同样值得一试的还有 Reddit's Node.js APIs, of which I personally liked Snoocore。
我已经尝试了很长时间了。 我想从包含成人内容的 subreddit 中删除内容。 但是,问题是,您必须先回答一个简单的问题,然后才能访问该页面,即您是否年满 18 岁。 我对源代码做了一些研究,发现解决方案是一个简单的 POST 请求。您需要在其中发送参数"over18=yes"。 但我的问题是,在 post.
之后我无法访问响应正文这是在节点中使用 http 请求的代码。我什至尝试过使用节点 "request" 模块,但也没有任何帮助。
希望能在这里找到可以帮助我的人。
var http = require("http");
var options = {
host: 'www.reddit.com',
port: 80,
path: '/over18?dest=http%3A%2F%2Fwww.reddit.com%2Fr%2Fnsfw&over18=yes',
method: 'POST'
};
var req = http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
// write data to request body
req.write('data\n');
req.write('data\n');
req.end();
这里是使用节点请求模块的代码
var request = require("request");
request.post({url:'http://www.reddit.com/over18?dest=http%3A%2F%2Fwww.reddit.com%2Fr%2Fnsfw', form: {over18:'yes'}}, function(err,httpResponse,body){
console.log(body);
});
我正在尝试访问的 URL 是 http://www.reddit.com/r/nsfw
简而言之,当您点击 YES 按钮时,表单将 over18=yes 参数发送到 url http://www.reddit.com/over18?dest=http%3A%2F%2Fwww.reddit.com%2Fr%2Fnsfw using POST method. Then, server responds with an 302 Redirection header, cookie with value over18=1 and finally redirects to url http://www.reddit.com/r/nsfw 使用 GET 请求。然后,服务器只检查您是否有具有所需值的 cookie。
您只需要使用 GET 方法使用 cookie 直接向最终 url 请求。
var request = require("request");
var target = "http://www.reddit.com/r/nsfw";
var jar = request.jar();
var cookie = request.cookie("over18=1");
cookie.domain = "reddit.com";
cookie.path = "/";
jar.setCookie(cookie, target, function(error, cookie) {
console.log(error);
console.log(cookie);
});
request({
uri: target,
method: "GET",
jar: jar
}, function(error, response, body) {
console.log(response.statusCode);
console.log(body);
});
我也 运行 在 ahem 做一些研究的时候加入了这个。这是我的版本:
var url = 'http://www.reddit.com/r/nsfw/';
var request = require('request');
request = request.defaults({jar: true });
request.post({
followAllRedirects: true,
url: 'http://www.reddit.com/over18?dest=' + encodeURIComponent(url),
form: {uh: '', over18: 'yes', }
}, function(err, httpResponse, html) {
…
});
同样值得一试的还有 Reddit's Node.js APIs, of which I personally liked Snoocore。