• With issues:
  • Use the search tool before opening a new issue.
  • Please provide source code and commit sha if you found a bug.
  • Review existing issues and provide feedback or react to them.

Description

How to reproduce

route.POST("/v1/complete", complete)

func complete(ctx *gin.Context, content string) {
...

if _, err := ctx.Writer.Write([]byte("\n\ndata: {\"completion\": \"" + strings.ReplaceAll(content, "\n", "\\n") + "\"}")); err != nil {
    fmt.Println("Error: ", err)
} else {
    ctx.Writer.Flush()
}

} 

Expectations

$ curl http://localhost:9000/v1/complete

data: {  "completion": "xxx"}
data: [DONE]

Actual result

$ curl -i http://localhost:9000/hello/world
<YOUR RESULT>

Environment

  • go version: 1.20+
  • gin version (or commit ref): v1.9.1
  • operating system: Linux amd64

Comment From: Rorke76753

hi any idea for this problem? how you resolve this issue

Comment From: bincooo

hi any idea for this problem? how you resolve this issue

The reason is that I forgot the response request header:

ctx.Writer.Header().Set("Content-Type", "text/event-stream")
ctx.Writer.Header().Set("Transfer-Encoding", "chunked")
ctx.Writer.Header().Set("Cache-Control", "no-cache")
ctx.Writer.Header().Set("Connection", "keep-alive")
ctx.Writer.Header().Set("X-Accel-Buffering", "no")