2017-02-06 4 views
1

requestライブラリを使用してサーバにhttpリクエストを送信しています(https://github.com/request/request)。nodejs/protractor内の1つのリクエストから別のリクエストにクッキーを転送/パスする

Unfornatellyリクエスト(おそらくクッキー)にデータがないため、私は400の応答を得ました。

let request = require('request'); 
request.post("https://some-server.com/asyncUserLogin", { 
      form: { 
       username: 'rapidtest', 
       password: 'rapidtest123', 
       TARGET_URL: targetUrl 
      }, 
      jar: jar1 
     }, (err, res, body) => { 
      /* 
      * I am trying to get cookies from this request 
      * and pass it to the next in multiple ways, 
      * here I am stripping cookie manually, 
      * and trying to set Cookie: header 
      */ 
      let cookies = jar1.getCookies("https://some-server.com/asyncUserLogin"); 
      let cookiesString: string = ""; 
      cookies.forEach(cookie => { 
       if(cookiesString.length > 0) 
        cookiesString += ";" 
       cookiesString += cookie.key + "=" + cookie.value; 
      }); 

      /* 
      * in this request I am trying to send POST to: 
      * https://some-server.com/getOauthCookie?grant_type=password_assertion&client_id=xxx-yyyy 
      * along with cookies retrieved in previous request, 
      * it should respone with 200 and some other cookies 
      */ 
      request.post("https://some-server.com/getOauthCookie", { 
       har: { 
        postData: { 
         mimeType: 'application/x-www-form-urlencoded', 
         params: [ 
          { 
           name: 'grant_type', 
           value: 'password_assertion' 
          }, 
          { 
           name: 'client_id', 
           value: 'xxx-yyyy' 
          } 
         ] 
        }, 
        headers: [ 
         { 
          name: 'Content-Type', 
          value: 'application/x-www-form-urlencoded' 
         }, 
         { 
          name: 'Cookies', 
          value: cookiesString 
         } 
        ], 
       } 
       jar: jar1 
      }, (err, res, body) => { 
      //this request fails 
     }); 
}); 

は、私が最初の要求からのクッキーを1秒に渡されていることを、確認するために、何をする必要があります。

これは、私はそれをやっている方法ですか?

何かが見つからない場合には、私が行っているリクエストを表示するにはどうすればいいですか(How to view request sent from node.js to server?)?

答えて

0

あなたは完全なレスポンスボディからの完全な要求にアクセスすることができます - response.requestをして、リクエストヘッダ

request(options, function (error, response, body) { 
    if (error) throw new Error(error); 
    // This will give you complete request 
    console.log(response.request); 
    //This gives you the headers from Request 
    console.log(response.request.headers['Cookies']); 

}); 
+0

こんにちはからクッキー値を抽出します。あなたの答えをありがとう。これは間違いなく複製されます。質問のトピックは、**あるリクエスト/レスポンスから別のリクエスト/レスポンスにクッキーを渡す方法です** :) –

+1

確かに..その後、私の答えを編集させてください。 – AdityaReddy

+0

かなり近いです。しかし、あなたの答えは、**連鎖されたリクエストで**のクッキーを**渡す方法ではありません:) –

関連する問題