Comments (5)
Sure, so I think you need to split up between data=...
(for operations
and map
) and files=...
(for doc.pdf
), something like this…
data = {
"operations": json.dumps(gql_query),
"map": """{
"file": ["variables.input"]
}""",
}
files = {
"file": ("doc.pdf", open("doc.pdf", "rb"), "application/pdf"),
}
response = httpx.post(..., data=data, files=files)
The multipart encoding occurs whenever files=...
is passed and non-empty, so in this case it will use whatever is in data
as additional fields in the multipart form data.
This behavior is actually not crystal clear in the docs I believe, could probably use a "You can also add additional fields by passing data=...
" in Sending Multipart Form Uploads.
from httpx.
There is a convoluted request that I only managed to send with MultipartEncoder
You’d need to be providing more details about exactly what you’re not able to do with httpx’s multiparty support in order for us to be able to help at all.
from httpx.
I just found this issue closed with no further comments. I'm currently migrating from requests
to httpx
but I don't see what is the replacement of requests-toolbelt
's MultipartEncoder
? There is a convoluted request that I only managed to send with MultipartEncoder
from httpx.
Agreed with Tom - right now our multipart support is provided by the files=...
parameter, which made this issue irrelevant eventually, but happy to read about situations this doesn’t help you solve fully.
from httpx.
So this is what I'm currently sending using requests
. In short, it is uploading a file to a GQL endpoint (ariadne)
gql_query = {
"query": """query (
$input: Upload!
) {...}
""", "variables": {"input": None} }
encoder = MultipartEncoder(
fields={
"operations": json.dumps(gql_query),
"map": """{
"file": ["variables.input"]
}""",
"file": ("doc.pdf", open("doc.pdf", "rb"), "application/pdf"),
}
)
requests.post("...", data=encoder, headers={"Content-Type": encoder.content_type})
I can make it work with httpx
by
httpx.post("...", data=encoder.to_string(), headers={"Content-Type": encoder.content_type})
I wasn't able to upload the file with plain requests
so I used the toolbelt. Maybe I missed an obvious alley for me to do this?
from httpx.
Related Issues (20)
- Requests for new public APIs HOT 9
- Install httpx without pulling anyio, trio and socks HOT 1
- Pressure test httpx Slower and slower HOT 4
- 502 Error When Using Global Proxy
- Shipping broken. "Could not find module httpx" even when installed.
- Authentification docs reference non-existing class HOT 2
- `httpx.AsyncClient` has much worse performance than `aiohttp.ClientSession` with concurrent requests HOT 22
- HTTPX and Celery \ GEvent Issue HOT 3
- Not support "no_proxy" including value of ipv6 prefix style
- http2协议发送请求会发送两次,第一次数据,第二次空包End Stream,如何将第二次空包的End Stream放到第一次包中
- Support IDNA2003 HOT 2
- How to implement failed retry
- BasicAuth vs BasicAuthentication HOT 2
- Httpx connection Nonetype error
- Change default encoding to utf-8 in `normalize_header_key` and `normalize_header_value` functions HOT 4
- httpx能支持get或者post请求的时候传入代理,而不是httpx.AsyncClient初始化的时候传入吗
- Random httpx.ConnectError
- How to force enable HTTP/2.0 HOT 5
- Module 'httpx' has no attribute 'BasicAuthentication' - Documentation is not up-to-date
- error: "HTTPError" has no attribute "response" [attr-defined] HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from httpx.