Problem integrating hero template with fasthttp

i am integrating hero template to fasthttp but localhost:8080 shows just like this

Untitled

but i need localhost:8080 shows just like this

-Alice
-Bob
-Tom

problem is html code is shown. My app.go is

package main
import (
"bytes"
"log"
"github.com/shiyanhui/hero/examples/app/template"
"github.com/valyala/fasthttp"
)
func main() {
requestHandler := func(ctx *fasthttp.RequestCtx) {
var userList = []string{
"Alice",
"Bob",
"Tom",
}
buffer := new(bytes.Buffer)
template.UserList(userList, buffer)
if _, err := ctx.Write(buffer.Bytes()); err != nil {
log.Printf("ERR: %s\n", err)
}
}
log.Fatal(fasthttp.ListenAndServe(":8080", requestHandler))
}

Just a guess as I do not know fasthttp, but I think you need to set an appropriate content type.

2 Likes

PS: You are generating three lists with a single item each, is this really the expected behaviour?

Hi,
the browser needs an info what type of content you will serve.
In you case before you set the buffer you need to set the proper header:
ctx.SetContentType("text/html; charset=utf8")
After that it should work.

here you can find more complex example:

Wow really thanks a lot

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.