Giter Club home page Giter Club logo

Comments (13)

tomchristie avatar tomchristie commented on August 24, 2024

Okay. Multipart encoded data can only handle string or File data. We could perfectly well either coerce a limited set of scalar types, or just coerce any inputs with str()

from httpx.

tomchristie avatar tomchristie commented on August 24, 2024

Would probably be a useful point of reference to stay in line with whatever urllib3 currently does.

from httpx.

ambrozic avatar ambrozic commented on August 24, 2024

https://github.com/urllib3/urllib3/blob/master/src/urllib3/filepost.py#L84-L90

urllib3 isn't doing anything particularly advanced either. i can make a PR with a similar solution if you are fine with it.

from httpx.

tomchristie avatar tomchristie commented on August 24, 2024

So looks like urllib3 only accepts str/byte values. What does requests do for that case? If it coerces to str, then any idea where it does that?

from httpx.

ambrozic avatar ambrozic commented on August 24, 2024

Most of the fiddly magic in requests happens in requests.models:_encode_files. Quite a bit of knowledge implemented which can help us. Prepared data is then sent to urllib.filepost:encode_multipart_formdata we've already checked above.

Another edge case i came across reading this code are None values in data which would have to be handled in http3.multipart:multipart_encode

from httpx.

tomchristie avatar tomchristie commented on August 24, 2024

Okay, so to keep in the with the behaviour I can see there, we should:

  • Ignore None values.
  • Cast any non-str/bytes to str.
  • Handle lists of args.

from httpx.

sethmlarson avatar sethmlarson commented on August 24, 2024

I already posted in the PR (didn't realize this was the issue for that PR until looking further down the list) but I'm not a fan of blindly casting objects that are passed in to strings and then shipping that. Why not raise an error in that situation?

from httpx.

ambrozic avatar ambrozic commented on August 24, 2024

my argument would be that data ends up as string in multipart form body anyway and interface should let user go as far as reasonably possible before raising errors.

from httpx.

tomchristie avatar tomchristie commented on August 24, 2024

There's no right answer here - either API could be an resonable design choice.

I think we should probably prefer to nail things down quite tightly - given that we've got properly typed interfaces all the way through, it'd be nice if we worked towards raising TypeError on any incorrect type passed to one of our public interfaces.

from httpx.

sethmlarson avatar sethmlarson commented on August 24, 2024

We should strive towards no typing.Any usage in public interfaces. :)

from httpx.

ambrozic avatar ambrozic commented on August 24, 2024

Does this imply this issue is a non-issue then? It already works as expected under current implementation. Should libraries using http3 convert passed values in data dict according to interface?

from httpx.

tomchristie avatar tomchristie commented on August 24, 2024

I guess there's still some issues to be resolved here:

  • Ensure we deal with list of str / list of bytes okay.
  • Ensure we raise TypeError on any dict values other than bytes|str|typing.List[bytes]|typing.List[str].
  • Ensure we raise TypeError on any dict keys other than str.

Slightly depends on exactly which points we intend to perform the type checking at.

from httpx.

sethmlarson avatar sethmlarson commented on August 24, 2024

Can this issue be closed now?

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.