-
Notifications
You must be signed in to change notification settings - Fork 63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to upload binary data files? #183
Comments
Update: httpbin has a |
Hi @mikeholler, Thanks for opening this issue! This looks related to #180, in that it is an issue with the library's default serialization strategy: before A workaround for now is to monkey-patch the from uplink.converters import StandardConverter
def pass_through_request_body_converter(self, type_, *args, **kwargs):
return lambda value: value
StandardConverter.create_request_body_converter = pass_through_request_body_converter |
@prkumar thank you for the timely response! The code snippet is great, too. Any idea, with this workaround, how I might adapt it to have one route that interprets the body as JSON and one that just passes it through as bytes in the same class? I love that you're prioritizing this work, too. It does seem like this is pretty fundamental behavior and I'm glad to see that you think so as well :) |
@mikeholler - Sure thing! If you need to accept both JSON and binary data, I would recommend you define two separate methods, one decorated with from uplink import Consumer, Body, post, json
class SampleApi(Consumer):
@post("post")
def create_binary(self, body: Body):
"""Post some binary data to httpbin's /post method"""
@json
@create
def create_json(self, body: Body):
"""Post some JSON data to httpbin's /post method""" (This example demonstrates the ability to extend consumer methods.) You could take this further and expose a third method to joins the behavior and picks the correct request based on the type of the given body: from uplink import Consumer, Body, post, json
class SampleApi(Consumer):
def create(self, body):
if isinstance(body, (bytes, bytearray)):
return self.create_binary(body)
return self.create_json(body)
@post("post")
def create_binary(self, body: Body):
"""Post some binary data to httpbin's /post method"""
@json
@create
def create_json(self, body: Body):
"""Post some JSON data to httpbin's /post method""" |
@prkumar that's a great workaround. I think you've misunderstood my use-case a little bit (I only want to except binary OR json in a method, mutually exclusive, but I do want to have a Consumer that has methods that do both). However, I am clear on exactly how to solve my problem now. Thank you for the help and I look forward to an update making this into |
Hello there,
I considered posting this to Gitter, but it does not seem very active so I wanted to post here instead.
I'm evaluating using
uplink
for our API clients, and so far it looks like a wonderful tool, checking all of the boxes I'd expect. However, we have a few routes that accept binary data (think simple document store), and I don't see a way to upload unstructured binary data using uplink. Here's what I tried:Then I start httpbin locally with
docker run --rm -p 80:80 kennethreitz/httpbin
, and fire up an interpreter and tried a few things (which you'll see below). I've combed the docs and cannot find any examples of arbitrary data being uploaded, and when I searchbinary
,bytes
,file
, orbytearray
in this project I didn't see much. I'd like to know how to do this, and I'm thinking it should probably be added to the project's documentation as well.Then, when this didn't work, I just wanted to try something with plain
requests
, which of course did work:Note the
'json'
field has1234
which I posted is binary, but it's just coincidentally rendering it as valid JSON (since it is). I don't know if httpbin has something for binary files, so this was what I thought I could use.The text was updated successfully, but these errors were encountered: