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

Skip setting 'Content-Type' on file upload, when multiple bodies are supported #947

Open
kgutwin opened this issue Jan 23, 2024 · 0 comments

Comments

@kgutwin
Copy link
Contributor

kgutwin commented Jan 23, 2024

Describe the bug
Form-style submissions to endpoints that support multiple different content types are missing the necessary "boundary" attribute on the Content-Type header.

POST https://.../items HTTP/1.1
Host: ...
Accept: */*
Accept-Encoding: gzip, deflate
Connection: keep-alive
User-Agent: python-httpx/0.25.2
Authorization: Bearer ...
Content-Type: multipart/form-data
Transfer-Encoding: chunked

Cause

{% if endpoint.bodies | length > 1 %}
{% for body in endpoint.bodies %}
if isinstance(body, {{body.prop.get_type_string() }}):
{% set destination = "_" + body.body_type + "_body" %}
{{ body_to_kwarg(body, destination) | indent(8) }}
_kwargs["{{ body.body_type.value }}"] = {{ destination }}
headers["Content-Type"] = "{{ body.content_type }}"
{% endfor %}
{% elif endpoint.bodies | length == 1 %}
{% set body = endpoint.bodies[0] %}
{{ body_to_kwarg(body, "_body") | indent(4) }}
_kwargs["{{ body.body_type.value }}"] = _body
{% if body.content_type != "multipart/form-data" %}{# Need httpx to set the boundary automatically #}
headers["Content-Type"] = "{{ body.content_type }}"
{% endif %}
{% endif %}

In the template above, lines 60-62 have an "if content-type is not multipart/form-data" condition since httpx sets content type and multipart form boundary properly when files are provided. The same condition needs to be applied around line 54, which occurs when multiple body types are supported.

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