私は簡単なサイトをリクエストしようとしていました。それは、HTMLテキストを取得する必要がありますが、それはこのhttpリクエストでnode.js 'request'ライブラリを使用するにはどうすればよいですか?
NPMモジュールここでは '' を取得:github.com/request/request
コード:
var fs = require('fs');
var request = require('request');
var options = {
url:'https://sample.site/phpLoaders/getInventory/getInventory.php',
encoding : 'utf8',
gzip : true,
forever: true,
headers: {
'Host': 'sample.site',
'Connection': 'keep-alive',
'Content-Length': '58',
'Cache-Control': 'max-age=0',
'Accept': '*/*',
'Origin': 'https://csgosell.com',
'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Referer': 'https://sample.site/',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
'Cookie': 'my-cookies from browser'
},
form: {
stage:'bot',
steamId:76561198284997423,
hasBonus:false,
coins:0
}
};
request.post(options,
function(error, response, body){
console.log(response.statusCode);
if (!error) {
fs.writeFileSync('site.html', body);
}
else{
console.log(error);
}
}
);
クローム要求:https://i.stack.imgur.com/zKQo5.png Nodejs要求:https://i.stack.imgur.com/yH9U3.png
違いはヘッダーにあります: :権限:csgosell.com :方法:POST:パス:/phpLoaders/getInventory/getInventory.php:scheme:https
いくつかのグーグルの後、私はそれがhttp2であることを確信し、別のエージェントのオプションに入れようとしましたが、何も変わりませんでした。
var spdy = require('spdy');
var agent = spdy.createAgent({
host: 'sample.site',
port: 443,
spdy: {
ssl: true,
}
}).once('error', function (err) {
this.emit(err);
});
options.agent = agent;
公式サイトのドキュメントをNPMあなたがここにHTTP/2を必要とする結論を出すにはどうすればよいですか? なぜこれらすべてのヘッダーが必要ですか? ここにAPIやウェブサイトのURLを投稿しないでください。 – DanielKhan
@DanielKhan、申し訳ありませんが、リクエストの具体的なケースでした。私はURLを削除されました – L1vet