Skip to content

Instantly share code, notes, and snippets.

@vanpav
Last active August 28, 2020 09:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vanpav/99958bd1b583df4b6c42290515d28bdd to your computer and use it in GitHub Desktop.
Save vanpav/99958bd1b583df4b6c42290515d28bdd to your computer and use it in GitHub Desktop.
Small wrapper around Axios lib for post requests in Django
import Promise from 'promise-polyfill';
window.Promise = window.Promise || Promise;
import axios from 'axios';
import qs from 'qs';
const DJANGO_DEFAULTS = {
xsrfCookieName: 'csrftoken',
xsrfHeaderName: 'X-CSRFToken',
headers: {
'X-Requested-With': 'XMLHttpRequest'
}
};
export const http = (config) => {
this.axios = axios.create({
...DJANGO_DEFAULTS,
...config
});
let fn = (config) => {
return this.axios.request({
...config,
data: qs.stringify(config.data || {})
});
};
['delete', 'get', 'head'].forEach(method => {
fn[method] = this.axios[method];
});
['post', 'put', 'patch'].forEach(method => {
fn[method] = (url, data, config) => (
this.axios[method](url, qs.stringify(data), config)
);
});
return fn;
};
export default http();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment