Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An example of making an AxiosRequestConfig object to do a multipart/form-data request
import axios, {
} from 'axios';
export const makeAxiosRequestConfig = (
method: Method,
path: string,
data?: { [key: string]: any },
params?: any
): AxiosRequestConfig => {
let headers: AxiosRequestConfig['headers'] = {};
let formData: FormData | undefined = undefined;
if (data) {
headers['Content-Type'] = 'multipart/form-data';
formData = new FormData();
Object.entries(data).forEach(([key, val]) => {
// HACK - make type happy…
const hackVal = val as string | File;
formData.append(key, hackVal);
return {
url: `${ENDPOINT}${path}`,
data: formData,

This comment has been minimized.

Copy link
Owner Author

@mrcoles mrcoles commented Apr 8, 2020

This handles both regular and form data, so you can send files to your server. You would use it like…

const cfg = makeAxiosRequestConfig('PATCH', '/api/foo', myFormData);
cons resp = await axios(cfg);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment