20

I'm using Swagger 2.0 and I have a problem to send multiple post parameters. I have a swagger error Operation cannot have a body parameter and a formData parameter and I don't know how to fix it. In my definition I have a body parameter and this parameter need a JSON format but a side I have other parameter like files to upload and filename.

How can I do to send body and formData parameters both ?

Here is the web service definition :

  /updateDatas:
    post:
      summary: Upadate datas
      description: |
        Update datas
      consumes:
        - multipart/form-data
      produces:
          - application/json
      parameters:
        - name: firstFileName
          in: formData
          description: First file name.
          required: true
          type: string
        - name: secondFileName
          in: formData
          description: Second file name.
          required: true
          type: string
        - name: datas
          in: body
          description: Json object informations.
          required: true
          schema:
            $ref: '#/definitions/Datas'
        - name: firstFile
          in: formData
          description: First file .jpg
          required: true
          type: file
        - name: clientFile
          in: formData
          description: Second file .jpg
          required: true
          type: file
      tags:
        - Application
      responses:
        '200':
          description: Uploaded
          schema:
            $ref: '#/definitions/Upload'
        '401':
          description: Unauthorized Bad Token
John
  • 4,711
  • 9
  • 51
  • 101

2 Answers2

13

According to the swagger specifications see, type:body and type:formData cannot exist together for the same operation.

S.I.
  • 3,250
  • 12
  • 48
  • 77
aK26
  • 151
  • 1
  • 7
  • 3
    what about Open API 3.0.0 ? https://swagger.io/docs/specification/describing-request-body/ Is JSON with Formdata now possible? – Gobliins Jan 25 '18 at 09:47
9

One way to resolve the problem is to set "datas" as form parameter with the type "file". Here is an example:

  parameters:
    - name: petId
      in: path
      description: ID of pet to update
      required: true
      type: integer
      format: int64
    - name: additionalMetadata
      in: formData
      description: Additional data to pass to server
      required: false
      type: string
    - name: file
      in: formData
      description: file to upload
      required: false
      type: file

Ref: https://github.com/swagger-api/swagger-codegen/blob/master/modules/swagger-codegen/src/test/resources/2_0/petstore.yaml#L257

UPDATE: body parameters and form parameters cannot co-exist: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameterObject

Body - The payload that's appended to the HTTP request. Since there can only be one payload, there can only be one body parameter. The name of the body parameter has no effect on the parameter itself and is used for documentation purposes only. Since Form parameters are also in the payload, body and form parameters cannot exist together for the same operation.

William Cheng
  • 10,137
  • 5
  • 54
  • 79
  • 2
    There is no other way for the type 'file' ? Because I don't want to use a file to past the JSON data – John Apr 26 '16 at 13:55
  • Thank you!, but what if additionalMetadata is a reference to a definition? – hmartos Dec 13 '16 at 09:00
  • Can you give an example of what the HTTP request's body/payload looks like (e.g. using https://gist.github.com)? – William Cheng Dec 13 '16 at 10:24
  • 1
    So the final conclusion is that, we can not use both body and formdata in same form. Right? – urfusion Apr 05 '17 at 13:26
  • @urfusion technically, you could do that with multipart/mixed. One part form encoded and another part json. I dont know how openapi spec supports writing this though. And it may be tricky to implement, depending on your web framework / language. – The Fool Jan 04 '22 at 22:41