Skip to content

Instantly share code, notes, and snippets.

@pastranastevenaz
Created March 30, 2020 05:23
Show Gist options
  • Select an option

  • Save pastranastevenaz/17b3fc2d016d3a8d67f7caad0cd38f19 to your computer and use it in GitHub Desktop.

Select an option

Save pastranastevenaz/17b3fc2d016d3a8d67f7caad0cd38f19 to your computer and use it in GitHub Desktop.
Making an http request to a secure API
<template>
<div>
<h1>Example
</div>
</template>
<script>
import axios from 'axios'
export default{
data(){
return{
jwtToken: '', // You'll have to either hard code this or get it from somewhere like state management
dataFromApi: '' // We will set this from the Api Response
}
},
methods: {
makeApiRequest: function () {
axios.request({
url: 'http://example.com/api/endpoint',
method: 'get',
headers: {
'Authorization': 'Bearer' + jwtToken
}
})
.then(response => {
console.log("Response: " + JSON.stringify(response.data));
this.dataFromApi = response.data.somecoolstring;
return true;
})
.catch(error => {
if(error.response.data.error.status_code == 401){
console.log("Status Code: " + JSON.stringify(error.response.data.error.status_code));
console.log("Status Message: " + JSON.stringify(error.response.data.error.message));
return false;
}
})
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment