Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

TypeError: Expected Bytes when trying to run Bear flask app

I tried to run the final app for the flask basics course in workspaces as well as in my local environment and am getting the following error when I try and submit the 'bear name' on the home page.

TypeError
TypeError: Expected bytes

Traceback (most recent call last)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/Library/Python/2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/Library/Python/2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/Library/Python/2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/adsouza/software/PyCharm/Bear/app.py", line 41, in save
response.set_cookie('character', data)
File "/Library/Python/2.7/site-packages/werkzeug/wrappers.py", line 1008, in set_cookie
self.charset))
File "/Library/Python/2.7/site-packages/werkzeug/http.py", line 920, in dump_cookie
value = to_bytes(value, charset)
File "/Library/Python/2.7/site-packages/werkzeug/_compat.py", line 106, in to_bytes
raise TypeError('Expected bytes')
TypeError: Expected bytes

7 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Looks like the problem is coming up in your save() function. Can you show me that?

Heres the function

@app.route('/save', methods=['POST'])
def save():
    flash("Alright! That looks awesome!")
    response = make_response(redirect(url_for('builder')))
    data = get_saved_data()
    data.update(dict(request.form.items()))
    response.set_cookie('character', data)
    return response

Could this have something to do with browser settings? As I mentioned in my question, I cant event get this to work on Workspaces on multiple machines.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

If you notice, in the video, we always use json.dumps() to turn the dict into a string. I have a feeling that's what's missing from your view.

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,468 Points

You appear to be running Python 2.7 which can behave different in Python 3.4.

As explained in the Werkzeug issue #436 on GitHub,

"... there is no difference in Python 2 between str and bytes, so there's no casting. to_bytes() returns a string. In Python 3 instead a str contains Unicode data, which is different from bytes, so there's casting. to_bytes() returns... bytes."

If your string is unicode will not work in Python 2.7 or in 3.4. You need to convert your string to bytes with something like:

bstring = bytes(string, 'utf-8')

@chris Looks like I pasted the wrong stack trace. I tried this on 3.4 as well as in workspaces and I get the same error.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,468 Points

Roger. However, with Python 3.4, all strings are unicode which still is different from bytes. So I would look to see if somewhere in your execution a unicode string is being passed which then causes the Expecting bytes error.

Hello! Apparently there are two mistakes in downloadable script:

<label for="{{ category }}-{{ choice }}_icon"><img src="/static/img/{{ category }} -{{ choice }}.svg"></label>
// An uneccessary space {{ category }__HERE__-{{ choice }}

and

 response.set_cookie(b'character', json.dumps(data)) # This line in app.py works, 
# there's no "json.dumps(data)" in project's workspace, it's just data

However after i've changed those changes the bear doesn't get dressed, although the choices i've made are checked after updating and the background is also the right colour

Dear Kenneth, i have just installed python3.6 and flask etc.....

If i want to submit a form i received this message:

Traceback (most recent call last):
  File "C:\Python_dev\BuidBear\lib\site-packages\flask\app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python_dev\BuidBear\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python_dev\BuidBear\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python_dev\BuidBear\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python_dev\BuidBear\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python_dev\BuidBear\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python_dev\BuidBear\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python_dev\BuidBear\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python_dev\BuidBear\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python_dev\BuidBear\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Python_dev\BuidBear\app.py", line 41, in save
    response.set_cookie('character', data)
  File "C:\Python_dev\BuidBear\lib\site-packages\werkzeug\wrappers.py", line 1016, in set_cookie
    self.charset))
  File "C:\Python_dev\BuidBear\lib\site-packages\werkzeug\http.py", line 946, in dump_cookie
    value = to_bytes(value, charset)
  File "C:\Python_dev\BuidBear\lib\site-packages\werkzeug\_compat.py", line 190, in to_bytes
    raise TypeError('Expected bytes')
TypeError: Expected bytes

How can i correct ?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Looks like it's expecting bytes when saving the cookie. Try changing response.set_cookie('character', data) to response.set_cookie(b'character', data)?

Dear Kenneth, i tried but the situation is same. I used the originally code what have you uploded. Stage3 Final.

Traceback (most recent call last):
  File "C:\Python36\lib\site-packages\flask\app.py", line 1994, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Python36\lib\site-packages\flask\app.py", line 1985, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Python36\lib\site-packages\flask\app.py", line 1540, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python36\lib\site-packages\flask\app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Python36\lib\site-packages\flask\app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Python36\lib\site-packages\flask\app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Python36\lib\site-packages\flask\_compat.py", line 33, in reraise
    raise value
  File "C:\Python36\lib\site-packages\flask\app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Python36\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Python_dev\BuidBear\app.py", line 41, in save
    response.set_cookie(b'character', data)
  File "C:\Python36\lib\site-packages\werkzeug\wrappers.py", line 994, in set_cookie
    self.charset))
  File "C:\Python36\lib\site-packages\werkzeug\http.py", line 905, in dump_cookie
    value = to_bytes(value, charset)
  File "C:\Python36\lib\site-packages\werkzeug\_compat.py", line 186, in to_bytes
    raise TypeError('Expected bytes')
TypeError: Expected bytes
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Can you post app.py somewhere that I can see it?

Hi Kenneth, I decided I start from the beginning...if I will face with this error, I will ask you again.

Thank you!

Hi

Now is working fine. I found a very little mistake in the project file wha i have downloded.

<label for="{{ category }}-{{ choice }}_icon"><img src="/static/img/{{ category }} -{{ choice }}.svg"></label>

An uneccessary space between category and choice.

Regards

Viktor