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

Allow Multipart Requests Without Attaching Files #3396

Open
1 task done
laipz8200 opened this issue Nov 8, 2024 · 1 comment
Open
1 task done

Allow Multipart Requests Without Attaching Files #3396

laipz8200 opened this issue Nov 8, 2024 · 1 comment

Comments

@laipz8200
Copy link

laipz8200 commented Nov 8, 2024

Currently, a multipart request can only be sent when the files parameter is non-empty. This restriction limits cases where users might want to send data using multipart mode without attaching any files, which is possible in tools like Postman.

Proposed Change:

Instead of requiring files to be non-empty, we could determine the need for a multipart request based on whether files is None rather than its emptiness. This would allow users to send multipart requests without attaching files if needed.

Example Use Case:

  • Sending form data using multipart mode without the need for a file upload.

Additional Context:

This change has been discussed and approved in the Q&A section with a maintainer. I’d be happy to work on this change.

I'd like to try to implement this feature, please assign this issue to me.


@sylann
Copy link

sylann commented Nov 29, 2024

There is a way to solve this problem with current versions of httpx.
The documentation does not exactly demonstrate this use case, but it still gives the hint.

https://www.python-httpx.org/quickstart/#sending-multipart-file-uploads

multipart_form_data = {
    "field1": (None, "data"),
    "field2": (None, "data"),
}
http.post("your-url", files=multipart_form_data)

None corresponds to the "filename" of the field, which does not exist in this case.

Or, supposing you already have some dict (be careful though, it must be dict[str, str | bytes]),
You could convert it to a proper format like so:

data = {
    "field1": "data",
    "field2": "data",
}
multipart_form_data = {k: (None, v) for k, v in data.items()}
http.post("your-url", files=multipart_form_data)

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

2 participants