Skip to content

Instantly share code, notes, and snippets.

@yi-mei-wang
Created October 16, 2019 08:09
Show Gist options
  • Save yi-mei-wang/506029ad36d2e4454bf2deee5f5e18bc to your computer and use it in GitHub Desktop.
Save yi-mei-wang/506029ad36d2e4454bf2deee5f5e18bc to your computer and use it in GitHub Desktop.
Syntax of an axios call (GET request)

Step 1:

Install axios npm install axios

Step 2:

Import axios import axios from 'axios';

Step 3:

Use axios ​ ​

axios
	// Specify the methods, common ones are GET and POST
	// The url of the endpoint
	.get("https://insta.nextacademy.com/api/v1/users")// When the request succeeds, do something with the response
	.then(resp => {
		// I like to console.log the resp to see what the data look like
		// This way I know what I can do to manipulate and interact with the data
		console.log("resp.data", resp.data);
		// Do something else with the data
		this.setState({
			users: [...resp.data]
		})
	})
	// When the request fails, do something with the error
	.catch(err => {
		console.log(err);
	});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment