Arrekin
Arrekin

Reputation: 95

Twisted - Request did not return bytes

I have basic twister app and i keep getting errors like that:

Request did not return bytes

Request:

Resource:

<main.MainPageDispatcher object at 0x7f049fa62be0>

Value:

'hello'

Everywhere, even in official docs' examples I see that string is returned and yet it not works for me. If I comment out first return and send bytes instead of string it is working. Can anyone help me understand how it works? If it has to be in bytes then why official guides are returning strings?

My code:

from twisted.web.server import Site
from twisted.web.static import File
from twisted.web.resource import Resource
from twisted.internet import reactor

class MainPageDispatcher(Resource):
    isLeaf = True
    def __init__(self):
        super().__init__()

    def render_GET(self, request):
        request.setHeader(b"content-type", b"text/html")
        return "hello"
        return bytes("hello", "utf-8")

root = MainPageDispatcher()
factory = Site(root)
reactor.listenTCP(8888, factory)
reactor.run()

Upvotes: 4

Views: 2277

Answers (1)

Mindaugas Jusis
Mindaugas Jusis

Reputation: 101

In python3 I'm using:

def render_GET(self, request):
    request.setHeader("Content-Type", "text/html; charset=utf-8")
    return "<html>Hello, world!</html>".encode('utf-8')

str.encode('utf-8') returns a bytes representation of the Unicode string

Upvotes: 10

Related Questions