Created
March 30, 2020 05:23
-
-
Save pastranastevenaz/17b3fc2d016d3a8d67f7caad0cd38f19 to your computer and use it in GitHub Desktop.
Making an http request to a secure API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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