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

Fix “object supporting the buffer API required” for multipart requests #60

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

Conversation

Envek
Copy link

@Envek Envek commented Feb 2, 2021

This fixes https://github.com/aidan-/httpie-aws-authv4/issues/10 (see this original issue for more details)

The problem

Sometimes body can be not only string, but some complex objects-generators like request_toolbelt's MultipartEncoder.

In that case following error will be thrown:

TypeError: object supporting the buffer API required

when trying to calculate body hash here:

payload_hash = hashlib.sha256(body).hexdigest()

where body is <MultipartEncoder: <generator object MultiValueOrderedDict.items at **0x7f7e9c69c970>>

How to test yourself:

pip install httpie httpie-aws-authv4
http --auth-type aws4 --auth REDACTED.execute-api.us-west-2.amazonaws.com \
     --multipart POST http://example.com/test field=value

Implementation notes

In case of MultipartEncoder, I had to materialize request body to string and replace request body with it, otherwise HTTPie would hang up undefinitely. I'm not good at Python, but suppose it is because we had emptied generator object for hash calculation and HTTPie waits for data from it to send in body but data would never come again.

@njedema
Copy link

njedema commented Sep 23, 2021

Nice fix @Envek!
@DavidMuller can you please merge this? Had to write my own impl because 0.4.3 doesn't work for any non-string payload (i.e. JSON)

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.

2 participants