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

Decode headers using utf-8 only if they are not str #1020

Merged
merged 1 commit into from
Jan 13, 2021

Conversation

GianOrtiz
Copy link
Contributor

httpie showed a strange behavior whenever I was using sessions with files uploads using multipart forms, returning the following error during execution:

http: error: AttributeError: 'str' object has no attribute 'decode'

So, I tested with debug:

> http --debug --session=test -f POST pie.dev/post name='John Smith' cv@~/files/data.xml
HTTPie 2.3.0
Requests 2.25.1
Pygments 2.7.4
Python 3.7.3 (default, Jul 25 2020, 13:03:44) 
[GCC 8.3.0]
/usr/bin/python3
Linux 4.19.0-13-amd64

<Environment {'colors': 256,
 'config': {'__meta__': {'about': 'HTTPie configuration file',
                         'help': 'https://httpie.org/docs#config',
                         'httpie': '0.9.8'},
            'default_options': []},
 'config_dir': PosixPath('/home/gian/.httpie'),
 'devnull': <property object at 0x7f8f26114e08>,
 'is_windows': False,
 'log_error': <function Environment.log_error at 0x7f8f26042378>,
 'program_name': 'http',
 'stderr': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='UTF-8'>,
 'stderr_isatty': True,
 'stdin': <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'>,
 'stdin_encoding': 'UTF-8',
 'stdin_isatty': True,
 'stdout': <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>,
 'stdout_encoding': 'UTF-8',
 'stdout_isatty': True}>

http: error: AttributeError: 'str' object has no attribute 'decode'

Traceback (most recent call last):
  File "/home/gian/.local/bin/http", line 8, in <module>
    sys.exit(main())
  File "/home/gian/.local/lib/python3.7/site-packages/httpie/__main__.py", line 11, in main
    exit_status = main()
  File "/home/gian/.local/lib/python3.7/site-packages/httpie/core.py", line 81, in main
    env=env,
  File "/home/gian/.local/lib/python3.7/site-packages/httpie/core.py", line 196, in program
    for message in messages:
  File "/home/gian/.local/lib/python3.7/site-packages/httpie/client.py", line 63, in collect_messages
    httpie_session.update_headers(request_kwargs['headers'])
  File "/home/gian/.local/lib/python3.7/site-packages/httpie/sessions.py", line 80, in update_headers
    value = value.decode('utf8')
AttributeError: 'str' object has no attribute 'decode'

After some investigation, I noticed the error was generated from the usage of the header returned from get_multipart_data_and_content_type containing the Content-Type header for the multipart form, which returns an encoder and the header as a string, at collect_messages as a byte in update_headers using the decode method to generate a string from it.

To solve this issue I added a new test and then modified the update_headers function to not decode the header if it is already a string.

@jkbrzt jkbrzt merged commit db685d5 into httpie:master Jan 13, 2021
@jkbrzt
Copy link
Member

jkbrzt commented Jan 13, 2021

@GianOrtiz Thanks for the fix & test case! 🥧

jkbrzt added a commit that referenced this pull request Jan 13, 2021
@GianOrtiz GianOrtiz deleted the session-upload branch January 13, 2021 21:47
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