Last active
December 6, 2021 05:44
-
-
Save margani/e60ab554274583971e2d6365c4c7fdf3 to your computer and use it in GitHub Desktop.
Axios api requests #axios #js
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
const response = await axios.get(url).catch((error) => error.response); | |
console.log({ response }); |
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
const axios = require('axios'); | |
const res = await axios.get('https://httpbin.org/get?answer=42'); | |
res.data.args; // { answer: 42 } |
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
const axios = require('axios'); | |
// httpbin.org gives you the headers in the response | |
// body `res.data`. | |
// See: https://httpbin.org/#/HTTP_Methods/get_get | |
const res = await axios.get('https://httpbin.org/get', { | |
headers: { | |
'Test-Header': 'test-value' | |
} | |
}); | |
res.data.headers['Test-Header']; // "test-value" |
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
const res = await axios.post('https://httpbin.org/post', { hello: 'world' }, { | |
headers: { | |
'Test-Header': 'test-value' | |
} | |
}); | |
res.data.headers['Test-Header']; // "test-value" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment