Golang serve "css" resource file as a text/plain

if can any one tell me what wrong about my coding:
it’s gives me that the css is transferred as text/plain

code :
if strings.HasSuffix(path, “.html”) {
contentType = “text/html”
} else if strings.HasSuffix(path, “.css”) {
contentType = “text/css”
} else if strings.HasSuffix(path, “.js”) {
contentType = “application/javascript”
} else if strings.HasSuffix(path, “.png”) {
contentType = “image/png”
} else if strings.HasSuffix(path, “.jpg”) {
contentType = “image/jpeg”
// http.ServeFile(w, r, r.URL.Path)
} else if strings.HasSuffix(path, “.gif”) {
contentType = “image/gif”
} else if strings.HasSuffix(path, “.svg”) {
contentType = “image/svg+xml”
} else if strings.HasSuffix(path, “.mp4”) {
contentType = “video/mp4”
} else if strings.HasSuffix(path, “.webm”) {
contentType = “video/webm”
} else if strings.HasSuffix(path, “.ogg”) {
contentType = “video/ogg”
} else if strings.HasSuffix(path, “.mp3”) {
contentType = “audio/mp3”
} else if strings.HasSuffix(path, “.wav”) {
contentType = “audio/wav”
} else {
contentType = “text/plain”
}

please if any one can help I am struggling with this error 2 days. It will be a great help.

Well, I’m just found a Solution to This problem
instead of serving requested Files with bufio reader
and using the w.Header().Add(“Content Type”, contentType)
i just use the http.ServeFile(w, req, path) to handle the req.

And Thank for everyone who tried to help.

First off, I don’t think this is an error strictly speaking, most browsers will happily ignore it in my experience. (Which doesn’t mean you shouldn’t fix it, you’re right)
Sadly your snippet is not enough to tell you where your error is.

Anyway, I think the proper way would be to do this in your HandlerFunc:

w.Header().Set("Content-Type", "yourMIMEType")

// edit
Ok, I was too slow. Glad you found a solution.

In case you are still wondering…
If what you wrote in your last comment was your actual code, I suspect your problem is
.Add("Content Type", the proper http header is Content-Type, please note the dash, it’s important, in my testing it doesn’t work without the dash.

1 Like

Thank ou Jatyn : yeah! I think that the code That i shared was not complete. And sorry for that.
Your setting works nice. .Add(“Content-Type”).
Happy to hear from somebody and get a response for my first post here. Thank u!

http.ServeFile will do the work, but if you do have a need to parse the MIME type, use https://golang.org/pkg/mime/#ParseMediaType or https://golang.org/pkg/mime/#TypeByExtension instead.

1 Like

Sometimes it may fail, you will need to add the appropriate mime type yourself.

Yeah that’s right … I just stick to the .Add(“Content-type”), It Works Fine For My Project…
( i want to build a little web app like CMS " just simple" no MVC and no Complicated code, to explain the concept to my students “how to assemble piece by piece and build a simple web app” :blush: , then i will go deep to a Photo Gallery web App to explain more concepts and some design pattern like MVC and more ) … this is what im working in… and thank you so much.

1 Like

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