Skip to content

Instantly share code, notes, and snippets.

@edebie
Forked from vanpav/http.js
Created August 28, 2020 09:20
Show Gist options
  • Save edebie/58d14beef113c59d1ed0bdf37c9b2bed to your computer and use it in GitHub Desktop.
Save edebie/58d14beef113c59d1ed0bdf37c9b2bed 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