Description
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.