Skip to content

Instantly share code, notes, and snippets.

@luizomf
Created March 31, 2022 12:26
Show Gist options
  • Save luizomf/d04adcd03e51e3c60f7eb8bcee998544 to your computer and use it in GitHub Desktop.
Save luizomf/d04adcd03e51e3c60f7eb8bcee998544 to your computer and use it in GitHub Desktop.
Exemplo de uso da API criada com Django Rest Framework com JavaScript.
(async function() {
console.clear();
const headers = {
'Content-Type': 'application/json',
};
const body = JSON.stringify({
"username": "joanadarc",
"password": "Abc@12345678"
});
const config = {
method: 'POST',
headers: headers,
body: body
};
const response = await fetch(
'http://127.0.0.1:8000/recipes/api/token/',
config
);
const json = await response.json();
console.log('STATUS', response.status);
console.log(json.access);
})();
(async function() {
console.clear();
const headers = {
// authorization: 'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNjQ4NzMyNzQ1LCJpYXQiOjE2NDg3MjkxNDUsImp0aSI6IjJiNDJhNGM2MDJmMjQ1ZjQ4ZTNlMjNjZTkyYWI1ZDdjIiwidXNlcl9pZCI6MTF9.I_D3Dw2CsHknxVSlYR1NK5EmIDu6EHoZKLkslUg_CtM'
};
const config = {
method: 'GET',
headers: headers,
};
const response = await fetch(
'http://127.0.0.1:8000/authors/api/me/',
config
);
const json = await response.json();
console.log('STATUS', response.status);
console.log(json);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment