Skip to content

[JAVASCRIPT] Problem with superagent and multipart/form-data #3929

@triptec

Description

@triptec
Description

Generating a javascript client with fileupload doesn't work if consumes is set to multipart/form-data. This seems to be a problem with superagent (forwardemail/superagent#746)

Swagger-codegen version

2.2.1

Swagger declaration file content or url
swagger: '2.0'
info:
  version: '1.0.0'
  title: API
  description: API
  contact:
    name: API
    email: api@api.se
    url: http://api.se
  license:
    name: MIT
    url: http://opensource.org/licenses/MIT
host: localhost
basePath: /
schemes:
  - http
consumes:
  - application/json
produces:
  - application/json
paths:
  /images:
    post:
      description: Create a new image
      operationId: createImage
      consumes:
        - multipart/form-data
      parameters:
        - name: image
          in: formData
          description: The image
          required: true
          type: file
          minLength: 1
          maxLength: 5000000  # ~5MB
      responses:
        '200':
          description: Returns the image information
          schema:
            $ref: '#/definitions/Image'
          headers:
            Location:
              type: string
              description: The URL of the newly-added image
        default:
          description: unexpected error
          schema:
            $ref: '#/definitions/ErrorModel'
      security:
        - api_key: []
definitions:
  Image:
    type: object
    required:
      - id
      - filename
      - mimetype
    properties:
      id:
        type: integer
      filename:
        type: string
      mimetype:
        type: string
  ErrorModel:
    type: object
    required:
      - code
      - message
    properties:
      code:
        type: integer
        format: int32
      message:
        type: string
Suggest a Fix

Changing the code in callApi to something like this solves the problem

    var contentType = this.jsonPreferredMime(contentTypes);

    if (contentType) {
      // Issue with superagent and multipart/form-data (https://github.com/visionmedia/superagent/issues/746)
      if(contentType != 'multipart/form-data'){
        request.type(contentType);
      }
    } else if (!request.header['Content-Type']) {
      request.type('application/json');
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions