Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doesn't support formData #299

Closed
NickeNicrad opened this issue Aug 5, 2022 · 4 comments
Closed

doesn't support formData #299

NickeNicrad opened this issue Aug 5, 2022 · 4 comments

Comments

@NickeNicrad
Copy link

No description provided.

@olignyf
Copy link

olignyf commented Nov 2, 2022

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;
  }
}

@eithe
Copy link
Contributor

eithe commented Nov 28, 2022

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: #288

@KeystoneScience
Copy link

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;
};

@markrickert
Copy link
Member

#288 has been merged and should fix this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants