apisauce icon indicating copy to clipboard operation
apisauce copied to clipboard

doesn't support formData

Open NickeNicrad opened this issue 1 year ago • 3 comments

NickeNicrad avatar Aug 05 '22 21:08 NickeNicrad

I do this if it's any help

  public upload<T>(
    url: string,
    params?: any,
    config?: AxiosRequestConfig,
  ): Promise<ApiResponse<T>> {
    if (!config) {
      config = {};
    }
    config.headers = { 'Content-Type': 'multipart/form-data' };
    const formData = new FormData();
    Object.keys(params).forEach((el: any) => {
      formData.append(el, params[el]);
    });
    const response = this.api.post<T>(url, formData, config);
    return response;
  }
}

olignyf avatar Nov 02 '22 17:11 olignyf

axios v0.27.0 has automatic serialization to FormData, but unfortunately apisauce hasn't been updated to that version.

Edit: This PR would fix that: https://github.com/infinitered/apisauce/pull/288

eithe avatar Nov 28 '22 08:11 eithe

here is my workaround to make a general post function that can take form data:

const post = apiClient.post;
apiClient.post = async (url, data, axiosConfig = {}, contentType) => {
  if (contentType === "multipart/form-data") {
    axiosConfig.headers = {
      ...axiosConfig.headers,
      "Content-Type": "multipart/form-data",
    };
    const formData = new FormData();
    Object.keys(data).forEach((key) => {
      formData.append(key, data[key]);
    });
    data = formData;
  }

  const response = await post(url, data, axiosConfig);
  return response;
};

KeystoneScience avatar May 25 '23 21:05 KeystoneScience

#288 has been merged and should fix this issue.

markrickert avatar Feb 21 '24 16:02 markrickert