Skip to content

[Typescript-Angular2] Setting multipart/form-data will always send [object FormData] as body #6457

@SargoDarya

Description

@SargoDarya
Description

At the moment it is not possible to actually send multipart/form-data requests using generated code with the typescript-angular2 generator as it will always call formParams.toString() for the body contents which will always result in [object FormData] in the actual request.

Swagger-codegen version

v2.3.0

Swagger declaration file content or url

Problematic file: swagger-codegen/modules/swagger-codegen/src/main/resources/typescript-angular/api.service.mustache

Problematic lines:

let formParams = new (useForm ? FormData : URLSearchParams as any)() as {
   set(param: string, value: any): void;
};
...
{{#hasFormParams}}
    body: formParams.toString(),
{{/hasFormParams}}
Steps to reproduce
  1. Create an API service which can upload files
  2. Generate the API client as usual using typescript-angular2 template
  3. Try to use the Service with a file
    => Request will show up with [object FormData] in the body
Suggest a fix/enhancement

Angulars HTTP handling of forms is actually pretty smart. I think simply changing the line to the following works.

{{#hasFormParams}}
    body: useForm ? formParams : formParams.toString(),
{{/hasFormParams}}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions