2017-05-08 18 views
19

मैं fetch के साथ एक साधारण बुनियादी प्रमाणीकरण लिखना चाहता हूं, लेकिन मुझे 401 त्रुटि मिल रही है। यह गज़ब का अगर कोई मुझसे कहता है क्या कोड के साथ गलत क्या है जाएगा:लाने के साथ मूल प्रमाणीकरण?

let base64 = require('base-64'); 

let url = 'http://eu.httpbin.org/basic-auth/user/passwd'; 
let username = 'user'; 
let password = 'passwd'; 

let headers = new Headers(); 

//headers.append('Content-Type', 'text/json'); 
headers.append('Authorization', 'Basic' + base64.encode(username + ":" + password)); 

fetch(url, {method:'GET', 
     headers: headers, 
     //credentials: 'user:passwd' 
     }) 
.then(response => response.json()) 
.then(json => console.log(json)); 
//.done(); 

function parseJSON(response) { 
return response.json() 
} 
+4

क्या यह ठीक है अगर शीर्षलेख में 'मूल' शब्द के बाद कोई स्थान नहीं है? – Orifjon

+0

ओह एफ ***, हाँ! धन्यवाद! पूरी तरह से याद किया ;-) –

उत्तर

30

आप Basic और इनकोडिंग यूज़रनेम और पासवर्ड के बीच एक रिक्ति याद कर रहे हैं।

headers.append('Authorization', 'Basic ' + base64.encode(username + ":" + password)); 
संबंधित मुद्दे