Httpx behaving differently than requests? #3118
-
the data paramater in httpx.post and httpx.client.post behave differently than requests.post Example with discord's API: This will not work import httpx
header = {
'Authorization': "Bot token",
}
files = {
"file": ("hi.png", open("./image.png", 'rb'), "image/png")
}
payload = {
"content": "pls work",
"allowed_mentions": {
"parse": []
}
}
channel_id = "1079501847720640682"
r = httpx.post(f"https://discord.com/api/v9/channels/{channel_id}/messages", data=payload, headers=header, files=files)
print(r.status_code)
print(r.text) However with requests, this will work fine import requests
header = {
'Authorization': "Bot token",
}
files = {
"file": ("hi.png", open("./image.png", 'rb'), "image/png")
}
payload = {
"content": "pls work",
"allowed_mentions": {
"parse": []
}
}
channel_id = "1079501847720640682"
r = requests.post(f"https://discord.com/api/v9/channels/{channel_id}/messages", data=payload, headers=header, files=files)
print(r.status_code)
print(r.text) I am unsure if this is a bug or if I need to pass these arguments differently in httpx |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 2 replies
-
Here is the console output
|
Beta Was this translation helpful? Give feedback.
-
the APIs are slightly different, it looks like |
Beta Was this translation helpful? Give feedback.
-
I mean it works fine if the dict passed to the data paramater only has one item in httpx, but if I have 2 or more keys it raises that error |
Beta Was this translation helpful? Give feedback.
-
Thanks for the question... Form data (URL encoded or Multipart) can only represent plain data. Everything on the wire is just a string, so we don't want There's scope for:
|
Beta Was this translation helpful? Give feedback.
-
Wait so then what would be the equivalent in httpx? Would I pass a url encoded string to data or..? |
Beta Was this translation helpful? Give feedback.
-
This discussion is probably about Discord API parameters. I'm interested in the Discord API, so I looked into it ... https://discord.com/developers/docs/reference#uploading-files
This solves the problem. import json
...
payload_json = {"payload_json": json.dumps(payload)}
r = httpx.post(f"https://discord.com/api/v9/channels/{channel_id}/messages", data=payload_json, headers=header, files=files) ✅ I don't know if the form data encoding of |
Beta Was this translation helpful? Give feedback.
-
I think Httpx shoud be like reqests to automatically decode the byte type in the form to the str type, |
Beta Was this translation helpful? Give feedback.
This discussion is probably about Discord API parameters.
I'm interested in the Discord API, so I looked into it ... https://discord.com/developers/docs/reference#uploading-files
This solves the problem.
✅
I don't know if the form data encoding of
requests
is correct 🤔