From 09a4396d11a55c2481d70699daabfc2c8d88c322 Mon Sep 17 00:00:00 2001 From: Romain Vincent Date: Fri, 29 Nov 2024 18:25:55 +0100 Subject: [PATCH] show how to send multipart form data without files As it is, the documentation hints at the possibility but it is not explicit. Hence it takes more time to figure it out on your own, especially for a less experienced developer. --- docs/quickstart.md | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/quickstart.md b/docs/quickstart.md index aa203a8336..9d05bbcde2 100644 --- a/docs/quickstart.md +++ b/docs/quickstart.md @@ -169,7 +169,7 @@ Form encoded data can also include multiple values from a given key. } ``` -## Sending Multipart File Uploads +## Sending Multipart File Uploads and/or Data You can also upload files, using HTTP multipart encoding: @@ -221,6 +221,23 @@ If you need to include non-file data fields in the multipart form, use the `data } ``` +If you need to send non-file data only, you can do so by using a tuple +of items for the field value without a filename: + +```pycon +>>> files = {'some-field': (None, "field-value")} +>>> r = httpx.post("https://httpbin.org/post", files=files) +>>> print(r.text) +{ + ... + "files": {}, + "form": { + "some-field": "field-value" + }, + ... +} +``` + ## Sending JSON Encoded Data Form encoded data is okay if all you need is a simple key-value data structure.