Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Content-Type in swagger spec is ignored #458

Open
mattdowdell opened this issue Apr 9, 2020 · 1 comment
Open

Content-Type in swagger spec is ignored #458

mattdowdell opened this issue Apr 9, 2020 · 1 comment

Comments

@mattdowdell
Copy link

I have an endpoint roughly defined as follows in my swagger spec, which I'm using the RequestsClient to intercat with underneath:

  /resource/{resourceID}:
    patch:
      tags:
        - my-tag
      summary: Updates resource
      description: 'Updates an existing resource by ID'
      operationId: updateResource
      consumes:
        - multipart/form-data
      produces:
        - application/json
      parameters:
        - in: path
          name: resourceID
          required: true
          type: string
          description: The ID of the resource to update
        - in: formData
          name: resourceConfigFile
          type: file
          required: false
        - in: formData
          name: resourceFoo
          type: string
          required: false
      responses:
        '204':
          description: Update successful
        '400':
          description: Bad request
          schema:
            $ref: '#/definitions/ErrorResponse'
        '404':
          description: kube client info not found
          schema:
            $ref: '#/definitions/ErrorResponse'
        '500':
          description: Internal / unexpected error
          schema:
            $ref: '#/definitions/ErrorResponse'

When I send a request to it without any of the required parameters, I get an error back from my server claiming that the Content-Type header is wrong. This is entirely correct as there's no content-type set at all:

send: b'PATCH /api/v2/example/resource/7a8e7f58-150a-41cb-ab3d-5910742761b0 HTTP/1.1\r\nHost: localhost:7070\r\nUser-Agent: python-requests/2.23.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: 0\r\n\r\n'

If I sent a request with the non-file optional parameter, I get a 415 unsupported media error back. This time it's because the content-type was set to application/x-www-form-urlencoded, where the request looks like this:

send: b'PATCH /api/v2/example/resource/7a8e7f58-150a-41cb-ab3d-5910742761b0 HTTP/1.1\r\nHost: localhost:7070\r\nUser-Agent: python-requests/2.23.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: 15\r\nContent-Type: application/x-www-form-urlencoded\r\n\r\n'
send: b'resourceFoo=bar'

Finally, if I actually send the file, the correct Content-Type is set and everything works as expected. In trying to workaround this, I tried to set the content type by hand and then went down the rabbit hole learning about setting the boundary and related things. In general, the advice seems to be to let the browser or underlying library do it for you.

At the moment, I can't tell if this is a bug in bravado, or something lower down. Reading over this suggests the correct thing to do if multipart/form-data is specified is to always use the files=... parameter when using requests.

@mattdowdell
Copy link
Author

It appears to be something in bravado-core that controls/expects this behaviour. When a file is added, the content-type is checked within the add_file function https://github.com/Yelp/bravado-core/blob/3f04ce5d9f5fa8deeff7f76f1f9025e5dd30e888/bravado_core/param.py#L319. I think it may be a case of if the consumes parameter is set to multipart/form-data then only the files parameter should be used in requests rather than falling back to the data parameter (see https://github.com/Yelp/bravado-core/blob/3f04ce5d9f5fa8deeff7f76f1f9025e5dd30e888/bravado_core/param.py#L148-L152)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant