Skip to content

Instantly share code, notes, and snippets.

@margani
Last active December 6, 2021 05:44
Show Gist options
  • Save margani/e60ab554274583971e2d6365c4c7fdf3 to your computer and use it in GitHub Desktop.
Save margani/e60ab554274583971e2d6365c4c7fdf3 to your computer and use it in GitHub Desktop.
Axios api requests #axios #js
const response = await axios.get(url).catch((error) => error.response);
console.log({ response });
const axios = require('axios');
const res = await axios.get('https://httpbin.org/get?answer=42');
res.data.args; // { answer: 42 }
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"
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