RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.67k stars 1.23k forks source link

CodeGeneration TypeScript does not regard 'multipart/form-data' header #3680

Open feelgood-interface opened 2 years ago

feelgood-interface commented 2 years ago

Good day, I am generating a TypeScript client (axios template) for the following documentation (i.e. I am trying to upload a file):

requestBody:
  content:
    multipart/form-data:
      schema:
        type: string
        format: binary

However, the 'multipart/form-data' content header or content_.getHeaders() are not added to the header options:

const content_ = new FormData();
content_.append("file", file.data, file.fileName ? file.fileName : "file");
let options_ = <AxiosRequestConfig>{
    data: content_,
    method: "POST",
    url: url_,
    headers: {
    },
    cancelToken
};
jurilents commented 1 year ago

Hey! Are there any updates? Has there been any progress on the issue?

btadros commented 1 year ago

This issue is crucial. Please update us!

mtsmith commented 1 year ago

I just ran into this issue as well. I had to add a hack as a workaround using an axios interceptor:

   image2(projectId: string, file: FileParameter | undefined, signal?: AbortSignal | undefined): Promise<void> {
        let url_ = this.baseUrl + "/api/project/{projectId}/image";
        if (projectId === undefined || projectId === null)
            throw new Error("The parameter 'projectId' must be defined.");
        url_ = url_.replace("{projectId}", encodeURIComponent("" + projectId));
        url_ = url_.replace(/[?&]$/, "");

        const content_ = new FormData();
        if (file === null || file === undefined)
            throw new Error("The parameter 'file' cannot be null.");
        else
            content_.append("file", file.data, file.fileName ? file.fileName : "file");

        let options_: AxiosRequestConfig = {
            data: content_,
            method: "POST",
            url: url_,
            headers: {
            },
            signal
        };

        return this.instance.request(options_).catch((_error: any) => {
            if (isAxiosError(_error) && _error.response) {
                return _error.response;
            } else {
                throw _error;
            }
        }).then((_response: AxiosResponse) => {
            return this.processImage2(_response);
        });
    }
instance.interceptors.request.use((config) => {
  if (config.url?.includes("/image")) {
    config.headers.set("Content-Type", "multipart/form-data")
  }
  return config;
});