Skip to content
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

Add a .bytes() method to HTTP responses #1095

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

benesch
Copy link

@benesch benesch commented Nov 27, 2024

The new .bytes() method returns the body as bytes rather than a string. It is modeled after the .json() method, which parses the body as JSON.

The use case is parsing HTTP requests that return binary data. Certain operations on strings in Starlark don't work correctly with binary (i.e., non-UTF-8 encoded) data. In particular, when strings contain non-UTF-8 binary data, the ord function returns 65533 (the codepoint for the Unicode replacement character) when called on a byte with a value between 0x80 and 0xff. ord functions as expected with bytes values.

The new .bytes() method returns the body as bytes rather than a string.
It is modeled after the .json() method, which parses the body as JSON.

The use case is parsing HTTP requests that return binary data. Certain
operations on strings in Starlark don't work correctly with binary
(i.e., non-UTF-8 encoded) data. In particular, when strings contain
non-UTF-8 binary data, the `ord` function returns 65533 (the codepoint
for the Unicode replacement character) when called on a byte with a
value between 0x80 and 0xff. `ord` functions as expected with bytes
values.
benesch added a commit to benesch/tidbyt-nyc-subway-zoom that referenced this pull request Nov 27, 2024
Since we're using a Protobuf API, pull the HTTP body as bytes rather
than a string. This works around weird behavior in ord.

Depends on tidbyt/pixlet#1095.
@benesch
Copy link
Author

benesch commented Nov 27, 2024

Here's the motivation for this in code (an app that parses the binary protobuf data in NYC subway feed directly): https://github.com/benesch/tidbyt-nyc-subway-zoom/pull/1/files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant