1

I am trying to implement fetch data using POST method with Basic authentication in JS . But I am receiving the 401 authorization error. I am using below code to implement that. Please let correct below code and me know where I am making mistack.

fetch('https://example.com', {
method: 'POST',
headers: { 'Content-Type': 'application/json'
    {Authorization: 'Username': 'testuset',
'Password': 'Testpassword'
}},
body: JSON.stringify({
    "FirstName": "test", 
    "Email": "testtt"
})
})
.then(response => response.text())
.then(contents => console.log(contents))
.catch(() => console.log("Can’t access " + url + " response. Blocked by browser?"))
  • Your format for the headers object is wrong. See this: https://stackoverflow.com/q/43842793/2930091 – Partik Jul 13 '20 at 02:17
  • 3
    Also, I just googled "fetch basic authentication" and the first result was a clear example of how Basic Authentication should be done with fetch (https://observablehq.com/@mbostock/fetch-with-basic-auth) – Partik Jul 13 '20 at 02:23

0 Answers0