2017-08-20 27 views
1

GET要求がRabbitMQ APIにヘルスチェックとして送信されているときに、認証ダイアログボックスを回避するために資格情報を渡すことができません。私はそれは、要求が[OK]を送信し、実際のURL内の資格情報をせずにこの要求を送信した場合フェッチ要求による資格情報の受け渡し方法

rabbitCol.js:12 Uncaught (in promise) TypeError: Failed to execute 'fetch' on 'Window': Request cannot be constructed from a URL that includes credentials: http://user:[email protected]:15672/api/aliveness-test/%2F 
    at rabbitColChecking (rabbitCol.js:12) 
    at allReqInt (allReqInt.js:5) 
    at HTMLAnchorElement.onclick ((index):108) 

- 私は (例えばhttp://user:[email protected]:15672/api/aliveness-test/%2F

内の資格情報を使用してURLを渡すと は、それが下のエラーを受け取ります認証ダイアログはUIにポップアップしてしまい、迷惑でもありません。

要求は以下の通りです -

var fetch = require('node-fetch'); 

    async function rabbitColChecking() { 
     let index; 
     const hostsRB = ['http://user:[email protected]:15672/api/aliveness-test/%2F', 'http://user:[email protected]:15672/api/aliveness-test/%2F', 'http://user:[email protected]:15672/api/aliveness-test/%2F', 'http://user:[email protected]:15672/api/aliveness-test/%2F]; 
     let lengthVal = hostsRB.length; 
     for(let hostIndxRB = 0; hostIndxRB < lengthVal; hostIndxRB++) { 
      index = hostIndxRB; 
      let url = hostsRB[hostIndxRB]; 
      fetch(url, {method: 'GET', credentials:'same-origin', redirect: 'follow', agent: null, headers: {"Content-Type": "text/plain"}, timeout: 5000} 
     ).then(function (hostindxRB, res) { 
       handleLedResponseRB(res, hostindxRB); 
      }.bind(null, hostIndxRB)); 
      await sleep(500); 
     } 
    } 

この要求を送信するためのトリガは、いくつかのHTMLファイル内の "onclickの" 機能です。

私はネットで見たすべてのソリューションを実際に試しましたが、このユースケースを解決するものは何もありません。

答えて

1

あなたはこのように、認証ヘッダーを使用してフェッチを使用して、ユーザー名とパスワードを送信することができます

fetch(url, { 
    method: 'GET', 
    credentials: 'same-origin', 
    redirect: 'follow', 
    agent: null, 
    headers: { 
     "Content-Type": "text/plain", 
     'Authorization': 'Basic ' + btoa('username:password'), 
    }, 
    timeout: 5000 
}); 

btoaは、ブラウザによって提供される機能です。サーバー側で使用する場合は、btoa npmモジュールでジョブを実行する必要があります。

+0

ありがとうございました。でも、それでもポップアップダイアログが表示されます。以下の更新されたコード行をご覧ください: fetch(url、{メソッド: 'GET'、モード: 'no-cors'、資格: 'same-origin'、リダイレクト: 'follow'、エージェント:null、ヘッダー:{" (ユーザー:パス)}、タイムアウト:5000} –

+0

便利な:コンテンツタイプ: "text/html、application/xhtml + xml、application/xml"、 "Authorization": "Basic {{トークン}}" + btoa答え!ありがとう、Patrick! 閉じ括弧が紛失している可能性がありますか? –

+0

実際、この点を指摘してくれてありがとうございます。 –

関連する問題