It's not issue but I can't solve it because of my little knowledge so please help me.

I'm trying to create API for providing images. And I want to get images through API url without iframes.

<img src={url} />

But gin's html always has html, head, body tags.

<html>
  <head></head>
  <body>
    <p>hello</p>
  </body>
</html>

I want to render only my tag. How do I remove them?

Comment From: liuliqiang

Hi @chihiro-yabuta , how do you render the response HTML?

I think the gin framework will not auto add the html and head tags.

Comment From: chihiro-yabuta

Thank you for your reply, @liuliqiang. I explain my api flow a little bit.

I want to get svg from gin's api, so I created some query.

svg := strings.ReplaceAll(c.DefaultQuery("raw", ""),  " ", "+")
//this is to set svg element in gin from react. this text is compressed by "compress/flate"
src := strings.ReplaceAll(c.DefaultQuery("src", ""),  " ", "+")
//this is to get svg element everywhere. You need to set compressed text.

therefore, I have svg data as string and I need to render from string, so I wrote like this.

c *gin.Context
---------------
s := "<style> * { margin: 0px; padding: 0px; }</style>"
html := fmt.Sprintf("<html>%s<body>%s</body></html>", s, d.Svg.DecSvg)
c.Writer.Write([]byte(html))

if I try to render without html tag, svg string is appeared to be wrapped by pre tag. and when I tested to render only p tag

c.Writer.Write([]byte("<p>hi</p>"))

html has those three tags automatically. so I think c.Writer is necessary to contain those.

are there any method to remove them from c.Writer? please teach me.

Comment From: liuliqiang

Hi @chihiro-yabuta ,

sorry, actually, I have not gotten your point for the issue, would you mind posting your code entirely here? Or PM I is also ok.

Comment From: chihiro-yabuta

thank you for your early reply, @liuliqiang.

this is my working directory. https://github.com/chihiro-yabuta/cardme/blob/dev-server/server/api/main.go and I wrote my code meaning a little bit, so reading issue may makes easier to understand. when you are comfortable, please read them. thank you😭

Comment From: liuliqiang

Hi @chihiro-yabuta , from your code and your describe, I think you mind want this:

switch mode {
    case "html":
        if d.Svg.DecSvg != "" {
            s := "<style> * { margin: 0px; padding: 0px; }</style>"
            html := fmt.Sprintf("<html>%s<body>%s</body></html>", s, d.Svg.DecSvg)
            c.Writer.Write([]byte(html))
        }
    case "plain_html":
        if d.Svg.DecSvg != "" {
            c.Writer.Write([]byte(d.Svg.DecSvg))
        }
    default:
        c.JSON(200, d)
}

Comment From: chihiro-yabuta

@liuliqiang yeah, but I can't render only c.Writer.Write([]byte(d.Svg.DecSvg)) because writer needs html tag.