Coder Social home page Coder Social logo

Multipart helper about httpx HOT 5 CLOSED

tomchristie avatar tomchristie commented on August 26, 2024
Multipart helper

from httpx.

Comments (5)

florimondmanca avatar florimondmanca commented on August 26, 2024 3

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.

tomchristie avatar tomchristie commented on August 26, 2024 1

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.

librehat avatar librehat commented on August 26, 2024

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.

florimondmanca avatar florimondmanca commented on August 26, 2024

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.

librehat avatar librehat commented on August 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.