0
http POST http://localhost:8000/api-auth-token/ username="saadman" password="Sakib123#"

It works fine when i run it on terminal.

Now, I am trying to implement equivalent code in javascript to fetch authtoken:

fetch('http://localhost:8000/api-auth-token/',{
    method:'POST',
    header:{
    username: 'saadman'
    password: 'Sakib123#'
  }
}

But it's not working

Andrew
  • 8,322
  • 2
  • 47
  • 70
  • You do not disclose what error(s) you are seeing. Judging by the code (and the answer) I'd assume that you are using fetch incorrectly - https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#supplying_request_options – Andrew Apr 01 '21 at 14:03

1 Answers1

1

finally this worked for me:

var raw = JSON.stringify({"username":"saadman","password":"Sakib123#"});

var requestOptions = {
  method: 'POST',
  headers:  {"Content-Type": "application/json"},
  body: raw,
  redirect: 'follow'
};

fetch("http://192.168.43.189:8000/api-auth-token/", requestOptions)
  .then(response => response.text())
  .then(result => console.log(result))
  .catch(error => console.log('error', error));