V
V
Vadim Rublev2020-01-20 15:31:28
go
Vadim Rublev, 2020-01-20 15:31:28

How to write Content-type to Go server HTTP response header?

Please help me to write Content-type in HTTP response header of Go-server!
I did this:
var contType = "text/css; charset=UTF-8"
w.Header().Add("Content-Type", contType)
but it doesn't seem to work... - browsers load files correctly after three times ( basically mistaking CSS for JS).
In general, the task is to ensure that the files sent by my Go-server are correctly recognized by browsers.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladislav, 2020-01-20
@Vadim Rublev

Are you calling
w.Header().Add("Content-Type", contType)
before calling
w.WriteHeader(status)
?
After the
w.WriteHeader(status) function,
writing something in the headers is useless.
Also, if you are serving files through the th server, use http.FileServer
https://gist.github.com/paulmach/7271283
it will set the Content-Type of the files for you.

A
Alexander Melentyev, 2020-01-20
@asmelentyev

Hey!
w.Header().Set("Content-Type", "application/json")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question