2016-10-13 3 views
0

私は簡単なサイトをリクエストしようとしていました。それは、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; 
+0

公式サイトのドキュメントをNPMあなたがここにHTTP/2を必要とする結論を出すにはどうすればよいですか? なぜこれらすべてのヘッダーが必要ですか? ここにAPIやウェブサイトのURLを投稿しないでください。 – DanielKhan

+0

@DanielKhan、申し訳ありませんが、リクエストの具体的なケースでした。私はURLを削除されました – L1vet

答えて

0

あなたの質問に答えるために、私はあなたのバックエンドアプリケーション(NodeJS)にあなたのフロントエンドアプリケーション(angularJS)からPOSTリクエストを受け取ることができますコード、そしてあなたを有効に別の機能の一部をコピー/ペーストします逆を行う別のアプリケーション(それはそれを消費する可能性がある)にnodeJSからPOSTリクエストを送信するには:

1)要求を受信すると、angularJSまたは何あなたのnodeJS内部アプリ

//Import the necessary libraries/declare the necessary objects 
var express = require("express"); 
var myParser = require("body-parser"); 
var app = express(); 
// we will need the following imports for the inverse operation 
var https = require('https') 
var querystring = require('querystring') 

     // we need these variables for the post request: 


     var Vorname ; 
     var Name ; 
     var e_mail ; 
     var Strasse ; 


    app.use(myParser.urlencoded({extended : true})); 
    // the post request is send from http://localhost:8080/yourpath 
    app.post("/yourpath", function(request, response) { 
     // test the post request 
     if (!request.body) return res.sendStatus(400); 

     // fill the variables with the user data 
     Vorname =request.body.Vorname; 
     Name =request.body.Name; 
     e_mail =request.body.e_mail; 
     Strasse =request.body.Strasse;  
     response.status(200).send(request.body.title); 

}); 

2から送信)の逆を実行します。 nodeJS applからPOST要求を送信する別のアプリケーション

function sendPostRequest() 
    { 
     // prepare the data that we are going to send to anymotion 
     var jsonData = querystring.stringify({ 
       "Land": "Land", 
       "Vorname": "Vorname", 
       "Name": "Name", 
       "Strasse": Strasse, 
      }); 
     var post_options = { 
      host: 'achref.gassoumi.de', 
      port: '443', 
      method: 'POST', 
      path: '/api/mAPI', 
      headers: { 
       'Content-Type': 'application/x-www-form-urlencoded', 
       'Content-Length': jsonData.length 
      } 
     }; 
     // request object 
      var post_req = https.request(post_options, function(res) { 
      var result = ''; 
      res.on('data', function (chunk) { 
       result += chunk; 
       console.log(result); 
      }); 
      res.on('end', function() { 
      // show the result in the console : the thrown result in response of our post request 
      console.log(result); 
     }); 
     res.on('error', function (err) { 
      // show possible error while receiving the result of our post request 
      console.log(err); 
     }) 
     }); 
     post_req.on('error', function (err) { 
      // show error if the post request is not succeed 
      console.log(err); 
     }); 
     // post the data 
     post_req.write(jsonData); 
     post_req.end(); 
// ps : I used a https post request , you could use http if you want but you have to change the imported library and some stuffs in the code 
    } 

にicationは最後に、私はこの答えの意志がノードJSとどのようnodeJSアプリケーションからのPOSTリクエストを送信するにはPOSTリクエストを取得する方法で探している人を助けたいと考えています。ポスト要求を受信する方法についての詳細は

ボディパーサーライブラリのNPMのドキュメントを読んでください:

+0

ありがとう男。新しいエージェントが追加されました: – L1vet

+0

var agent = new https.Agent({keepAlive:true、keepAliveMsecs:10000}); それは正常です – L1vet

関連する問題