Skip to content

[JavaScript] Client sets Content-Type for multipart/form-data which causes a missing boundary in the header #3878

@Danielku15

Description

@Danielku15
Description

When uploading files via Ajax and FormData, you must not set the Content-Type header which is done here. This will cause the browser to really send a header Content-Type: multipart/form-data which is wrong. If you do not set this header explicitely it will set the header to something like Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryhwk47FtsDeemWl3V. So basically the boundary is missing which makes it impossible for the server side to parse the body.

Swagger-codegen version

Latest

Swagger declaration file content or url

PetStore sample is also affected.
https://github.com/swagger-api/swagger-codegen/blob/master/samples/client/petstore/javascript/src/ApiClient.js#L379

Command line used for generation

Default parameters, see PetStore. We also use browserify to really use it in the browser, not in node.js.

Steps to reproduce

Define a file parameter on your API, make a call using the generated API. When checking the Content-Type header on the server-side you will see the missing boundary.

Related issues

swagger-api/swagger-editor#666
swagger-api/swagger-editor#995
swagger-api/swagger-ui#662

Suggest a Fix

In this if-block the first statement should be:

    } else if (contentType == 'multipart/form-data') {
      request.unset('Content-Type'); // unset content type for auto generation by browser
      var _formParams = this.normalizeParams(formParams);
      for (var key in _formParams) {

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions