Multipart uploading: explicit boundary ignored? #2235
Unanswered
los-floppos
asked this question in
Potential Issue
Replies: 1 comment 4 replies
-
This workaround works for me: #!/usr/bin/env python3
import json
import httpx
import curlify2
data = {
"integer": 123,
"boolean": "True",
"list": [
"a",
"b",
"c"
]
}
json_string = json.dumps(data)
with open("foo.json", "w") as i :
json.dump(json_string, i)
client = httpx.Client()
boundary = b'+++'
content_type_request = "multipart/form-data; boundary=" + boundary.decode('utf-8')
multipart_header_json = b'application/json; charset=UTF-8'
# Constructing the binary message body
file = open("foo.json", "rb")
content = b'--' + boundary + b'\n' + \
multipart_header_json + file.read() + b'\n' + \
b'--' + boundary + b'--\n'
r = client.build_request(
method="POST",
url="https://example.com",
content=content,
headers={"cacheControl": "3600", "content-type": content_type_request},
)
print(curlify2.to_curl(r))
response = client.send(r) |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hey,
I am trying to POST a request to upload a
JSON
file with multipart encoding.This post by @dreinon helped me quite a bit in figuring out how to set the boundary properly. However, still, my request looks error-prone. What puzzles me most is that the explicit set boundary
+++
seems to be ignored and a random string is used instead.Here is my sample code:
print(curlify2.to_curl(r))
returns:print(f"{boundary=}")
returns:boundary='+++'
Any suggestions are very much appreciated.
Thank you very much in advance 🙂
Beta Was this translation helpful? Give feedback.
All reactions