How to write a Content-type in HTTP response header Go-servers?

Help to record the Content-type in HTTP response header Go server, pliz!
Have done so:
var contType = "text/css; charset=UTF-8"
w.Header().Add("Content-Type", contType)
but it seems it doesn't work... browsers download their files right through three times (mostly take CSS over JS).
General objective: to be send to my Go server files correctly identifiable browsers.
April 4th 20 at 13:00
2 answers
April 4th 20 at 13:02
Solution
You call
w.Header().Add("Content-Type", contType)
before call the function
w.WriteHeader(status)
?

After the function
w.WriteHeader(status)
to write something in the headers useless.

So, if you are distributing files via the server, use http.FileServer
https://gist.github.com/paulmach/7271283
he put down for you Content-Type the files.
I do not give out files. Just send soprovojdali following HTML page (which it is attached and is requested by a browser).

> before call the function w.WriteHeader(status)
Oops.... I missed. I'm just after sending a file write (or think of writing) in the HTTP header it tipicamente. - Kayleigh.McClure9 commented on April 4th 20 at 13:05
@Kayleigh.McClure9need to record the headers to the status and prior to sending the file. - Kyler.Jaco commented on April 4th 20 at 13:08
Understood. Messed up the order of the code means. - Kayleigh.McClure9 commented on April 4th 20 at 13:11
The entry in the title tobagonian BEFORE sending the file - didn't help. (
Can't Google about the function WriteHeader()... Can you explain the meaning of the record status? (For example, the status of "200" I have a server sends a gun; unless other statuses he also won't send?) And whether it is necessary to write to the header tobagonian (i.e. decide whether its addition to the code my problem)? - Kayleigh.McClure9 commented on April 4th 20 at 13:14
@Kayleigh.McClure9, if you do not record the status ourselves, will write you a 200 status, all other statuses you have to register yourself.
That's why I advised http.FileServer, it will do a lot of work when serving files, I don't understand why it doesn't suit you. - Kyler.Jaco commented on April 4th 20 at 13:17
> he will do a lot of work when serving files, I don't understand why it doesn't suit you.
This is a separate issue. Maybe because I don't know how to work with him; may be because I have a complex router (which is making it easier for me was to use http.ServeFile)...
Now solve the problem with detection of browsers my files. - Kayleigh.McClure9 commented on April 4th 20 at 13:20
That's from the Opera:
5e25db838ec5c314357926.png
The browser shows the JS file, the code from the CSS file. For some reason! And surprised that there is a curly bracket out of place... (( - Kayleigh.McClure9 commented on April 4th 20 at 13:23
@Kayleigh.McClure9, can you give an example code?
As I understand it, you send in a single connection multiple files, so it is impossible, the browser must request the desired information, otherwise he will not understand what you sent to him. - Kyler.Jaco commented on April 4th 20 at 13:26
@Kayleigh.McClure9, if you want to make http2 server push, read this article https://habr.com/ru/post/325370/
but then, all clients should go to you with http2 - Kyler.Jaco commented on April 4th 20 at 13:29
> As I understand, you send in a single connection multiple files, so you can not
Not. My server handles each request. Here is the code (snippet to send CSS and JS):
if elemEndTwoPath == "css" {
var contType = "text/css; charset=UTF-8"
w.Header().Add("Content-Type", contType)
http.ServeFile(w, r, FileName_CSS) // serve the file.
return
}
if elemEndTwoPath == "js" {
var contType = "text/javascript; charset=UTF-8"
w.Header().Add("Content-Type", contType)
http.ServeFile(w, r, FileName_JS) // serve the file.
return
} - Kayleigh.McClure9 commented on April 4th 20 at 13:32
@Kayleigh.McClure9, and for what you are doing so, and not give up as accepted?
due to the fact that you decided to come up with their own method of communication between server and browser, to help you no one can except you.
See where you missed a return or something that causes the data to overlap each other.

My advice for the future, always use common standards, which have already thought up for you, this will simplify life for themselves and others, who will delve into your code. - Kyler.Jaco commented on April 4th 20 at 13:35
It's all clear. No your method I'm not making this up.
Snooping http.FileServer() - could not deal with him. (( Was using http.ServeFile().
What is wrong in my code (which is cited above), gives files and writing tipscontact in the HTTP header? - Kayleigh.McClure9 commented on April 4th 20 at 13:38
http.FileServer() gives files from the specified directory, right?
He would give my (connected) CSS / JS? Specified in HTML'e (i.e. URL, e) path? Ie like this: URL = site.com/css/my.css - returns from the server the file $/css/my.css. Right? - Kayleigh.McClure9 commented on April 4th 20 at 13:41
April 4th 20 at 13:04
Hi!
w.Header().Set("Content-Type", "application/json")
Hi! Thank you! - Kayleigh.McClure9 commented on April 4th 20 at 13:07

Find more questions by tags GoHTTP headers