PythonNest/PyNest

Why can't I serve html from the controllers?

TownedCrown opened this issue · 1 comments

Hi,

I wanted to try to return a simple HTML fragment.


@Controller("test")
class TestController:
    
    @Get("/")
    def get_tests(self):
        return """
        <h1>Hello</h1>
        """

On page load I expected a formatted html page.

Instead I got the string formatted.

How would I go about returning HTML responses instead of string ones?

Hi @TownedCrown, That's a great question!

Since PyNest is using FastAPI under the hood, there are a few ways you can implement it as explained here => -

from fastapi.responses import HTMLResponse


@Controller("test")
class TestController:
    
    @Get("/", response_class=HTMLResponse)
    def get_tests(self):
        return """
        <h1>Hello</h1>
        """

Or in another syntax -

from fastapi.responses import HTMLResponse


@Controller("test")
class TestController:
    
    @Get("/")
    def get_tests(self):
        html_content =  """
        <h1>Hello</h1>
        """
        return HTMLResponse(content=html_content, status_code=200)