0

i have added an input field which takes token as input and fetches a page with authenctication required and sends that token as header but this doesnt seem to work..all i want to do is take token as input from frontend page and give access to pages instead 0f using POSTMAN

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>

<body>
  <form>
    <input type="text" id="token" placeholder="token">
    <button type="button" onclick="myFun()"> submit</button>

  </form>
</body>
<script>
  function myFun() {
    var token = document.getElementById("token");
    object = {
      method: 'GET',
      headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json',
        'Authorization': 'Bearer' + token
      }
    }
    fetch('http://127.0.0.1:8000/app/task-list/', object)
</script>

</html>
5
  • 2
    You need a space between Bearer and your token (i.e. 'Bearer ' + token)
    – Cully
    Jun 5, 2020 at 6:27
  • 'Authorization': 'Bearer ' + token
    – Yousaf
    Jun 5, 2020 at 6:28
  • tried it but it didnt help Jun 5, 2020 at 6:33
  • Have you checked if you're getting the token (i.e. console.log(token))?
    – Cully
    Jun 5, 2020 at 7:06
  • I checked it in console..it is being received and fetch is sending all the data to that url ..even then its creating a 401/unauthorized error...error is being generated by that url...as if its not permitting a fetch request Jun 5, 2020 at 14:12

1 Answer 1

0
var token=document.getElementById("token");

This selects token id element. You need to fetch the value of the input.

var token=document.getElementById("token").value;

This will take the value of input box.

'Authorization': 'Bearer' + token

And Add space after Bearer.

'Authorization': 'Bearer ' + token

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.