FAQ
I've made a little web server and one page but i doesn't understand why the
output is not screened or printed as html :

package main

import (
"fmt"
"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Bonjour <br><a href='irc://10.0.0.1/42'>Serveur Irc sur
port 667 ip : 10.0.0.1 channel principal #42</a><br><a href='sftp://
pirate:pirate@10.0.0.1'>Server de Partage de fichier en sftp sur le port 22
avec id : pirate et mdp : pirate </a>")
}

func main() {
http.HandleFunc("/", handler)
http.ListenAndServe(":80", nil)
}

It writes me :

Bonjour <br><a href='irc://10.0.0.1/42'>Serveur Irc sur port 667 ip :
10.0.0.1 channel principal #42</a><br><a
href='sftp://pirate:pirate@10.0.0.1'>Server de Partage de fichier en
sftp sur le port 22 avec id : pirate et mdp : pirate </a>


Regards

Bussiere





"Les nouvelles technologies offrent pleins de nouvelles possibilités,
pleins de possibilités d'erreurs surtout en fait."
insurance.aes256 : http://goo.gl/gHyAY
-----BEGIN PGP PUBLIC KEY BLOCK-----
Version: GnuPG v1.2.1 (MingW32) - WinPT 0.7.96rc1

mQGiBE9sjdsRBADsY+DaDgj+pDuGLNVL6Dc+ghCxaEBK+9nwBvKPGIUocz13V42P
5Jn/NJ9zRgZNdg5XbhohN2ltas8Nr09rrRQXbBk8zjCg/Qr0a4LmfA3j128SwCfZ
aTe5zlo/WSSuGv6kWGe/KZrLmiQoZRTDbZzqWyZxuJRkcpgsZc+jYRXJHwCgualT
LpC23Ja3plM6J6noFaqAlMUEAKZGDadSS/BW4gckw9XqSHRex7/XyWU4sqVQlhMy
5DEIba7H8cU6grww+p/cYa3OCocv5Mycf6MbWDCPrtsG0X08u8PgEaE29BsLIgSm
JF6r8y4Rgwj5xyCMTaxql82S+tZHyScpAoQ0a+xlTwxMZMuwK9p6b7oipXHL+UPN
sDfkBACoYLS4PQPFotqWI96j83NNLwEumUzA6Rqyas8w6Xwr51quA65y/0nBmMmp
l58tKN7eyc5+bIpRX5bGG6ktCUHnfzk9uvnG7peilWzYT6+AJ3gbBgMGLBPByJBN
NH+nLjzsh4PzIxuSHLMiK5FkDNE2B95kaahIg42SQPT80LyvDLQlYnVzc2llcmVf
cHJpdmF0ZSA8YnVzc2llcmVAZ21haWwuY29tPohZBBMRAgAZBQJPbI3bBAsHAwID
FQIDAxYCAQIeAQIXgAAKCRAYpzZWoPP7b0h0AJ4n9feBF2FH4JUGhLU3XL3eIrOX
9gCfZbN4JHYl/nRLL4uHrceRUk1M9Pm5Ac0ET2yN3hAHAK5MypLMT50yklt1n/pQ
MjrFXuvM9IQvVyQemHMUBqKqxPTVMJLn86mR5mTLMqs+WVAqei8GTsQeSSvHyIbk
8RvYrKY7aK2RVZUV1dHQ6PII0w3BI5C0L9GM/UyIq0VRXTW6zjp9hS/xgn5SrQad
VIdGQKFVJa9h5/AzgjseARMQRkTQA/xX5/WmHxXaXUPhGxCeFsoqs53UFz1rkHYx
mus2WiD0gHbJ58DiMlVmupeNumi4e9wa+mYuW4qGwjzsQn9ZFNdoiUFzQrqZa6o+
Son+yZpGe/H9VidrWR5LhwD7AAMGBv9bhswvwpQ23X0kGItv7/jHVr/RJui7FAXY
mOM4WAoUv/5ttPHf8OifWmeUDPlzm4txvR5a7t4hhVDehek1J7FEVvfG+ffcHiPP
kkFaUv2EgrXziozt/Vkr2nngsKO5FpRJe9GSqn68uZirhwbkHSKX6LPNFTWSYR3E
fefnsCaP82OJDBFGFMg2aMsz9QL85bg58fgHU972kLTePXUVAh3gaxb6qpjwZt2b
yrCGJ/rnmIc/IWH8TJtHI5Yf3Abx2vlFTtzRH0CwVXuO0msjcfRrrrrS8NSm/ih5
0ssElxKU6IhGBBgRAgAGBQJPbI3eAAoJEBinNlag8/tvoBEAn2Yqu0UFC5OSjD1Y
JQ00FFjNVestAJ0XctcbmhKaFuQ46SccW8C2JDG/xA==
=+VZi
-----END PGP PUBLIC KEY BLOCK-----

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Dougx at Mar 4, 2013 at 2:57 pm
    You must set the content type, or the browser will treat the output as
    text.

    Like this:

    func thing(w http.ResponseWriter, model map[string]interface{},
    w.Header().Add("Content-Type", "text/html") // <--- This
    fmt.Fprintf(w, "Bonjour <br>.... ")
    }

    ~
    Doug.
    On Monday, March 4, 2013 5:29:48 PM UTC+8, bussiere adrien wrote:


    I've made a little web server and one page but i doesn't understand why
    the output is not screened or printed as html :

    package main

    import (
    "fmt"
    "net/http"
    )

    func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Bonjour <br><a href='irc://10.0.0.1/42'>Serveur Irc sur
    port 667 ip : 10.0.0.1 channel principal #42</a><br><a href='sftp://
    pirate...@10.0.0.1 <javascript:>'>Server de Partage de fichier en sftp
    sur le port 22 avec id : pirate et mdp : pirate </a>")
    }

    func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":80", nil)
    }

    It writes me :

    Bonjour <br><a href='irc://10.0.0.1/42'>Serveur Irc sur port 667 ip : 10.0.0.1 channel principal #42</a><br><a href='sftp://pirate...@10.0.0.1 <javascript:>'>Server de Partage de fichier en sftp sur le port 22 avec id : pirate et mdp : pirate </a>


    Regards

    Bussiere





    "Les nouvelles technologies offrent pleins de nouvelles possibilités,
    pleins de possibilités d'erreurs surtout en fait."
    insurance.aes256 : http://goo.gl/gHyAY
    -----BEGIN PGP PUBLIC KEY BLOCK-----
    Version: GnuPG v1.2.1 (MingW32) - WinPT 0.7.96rc1

    mQGiBE9sjdsRBADsY+DaDgj+pDuGLNVL6Dc+ghCxaEBK+9nwBvKPGIUocz13V42P
    5Jn/NJ9zRgZNdg5XbhohN2ltas8Nr09rrRQXbBk8zjCg/Qr0a4LmfA3j128SwCfZ
    aTe5zlo/WSSuGv6kWGe/KZrLmiQoZRTDbZzqWyZxuJRkcpgsZc+jYRXJHwCgualT
    LpC23Ja3plM6J6noFaqAlMUEAKZGDadSS/BW4gckw9XqSHRex7/XyWU4sqVQlhMy
    5DEIba7H8cU6grww+p/cYa3OCocv5Mycf6MbWDCPrtsG0X08u8PgEaE29BsLIgSm
    JF6r8y4Rgwj5xyCMTaxql82S+tZHyScpAoQ0a+xlTwxMZMuwK9p6b7oipXHL+UPN
    sDfkBACoYLS4PQPFotqWI96j83NNLwEumUzA6Rqyas8w6Xwr51quA65y/0nBmMmp
    l58tKN7eyc5+bIpRX5bGG6ktCUHnfzk9uvnG7peilWzYT6+AJ3gbBgMGLBPByJBN
    NH+nLjzsh4PzIxuSHLMiK5FkDNE2B95kaahIg42SQPT80LyvDLQlYnVzc2llcmVf
    cHJpdmF0ZSA8YnVzc2llcmVAZ21haWwuY29tPohZBBMRAgAZBQJPbI3bBAsHAwID
    FQIDAxYCAQIeAQIXgAAKCRAYpzZWoPP7b0h0AJ4n9feBF2FH4JUGhLU3XL3eIrOX
    9gCfZbN4JHYl/nRLL4uHrceRUk1M9Pm5Ac0ET2yN3hAHAK5MypLMT50yklt1n/pQ
    MjrFXuvM9IQvVyQemHMUBqKqxPTVMJLn86mR5mTLMqs+WVAqei8GTsQeSSvHyIbk
    8RvYrKY7aK2RVZUV1dHQ6PII0w3BI5C0L9GM/UyIq0VRXTW6zjp9hS/xgn5SrQad
    VIdGQKFVJa9h5/AzgjseARMQRkTQA/xX5/WmHxXaXUPhGxCeFsoqs53UFz1rkHYx
    mus2WiD0gHbJ58DiMlVmupeNumi4e9wa+mYuW4qGwjzsQn9ZFNdoiUFzQrqZa6o+
    Son+yZpGe/H9VidrWR5LhwD7AAMGBv9bhswvwpQ23X0kGItv7/jHVr/RJui7FAXY
    mOM4WAoUv/5ttPHf8OifWmeUDPlzm4txvR5a7t4hhVDehek1J7FEVvfG+ffcHiPP
    kkFaUv2EgrXziozt/Vkr2nngsKO5FpRJe9GSqn68uZirhwbkHSKX6LPNFTWSYR3E
    fefnsCaP82OJDBFGFMg2aMsz9QL85bg58fgHU972kLTePXUVAh3gaxb6qpjwZt2b
    yrCGJ/rnmIc/IWH8TJtHI5Yf3Abx2vlFTtzRH0CwVXuO0msjcfRrrrrS8NSm/ih5
    0ssElxKU6IhGBBgRAgAGBQJPbI3eAAoJEBinNlag8/tvoBEAn2Yqu0UFC5OSjD1Y
    JQ00FFjNVestAJ0XctcbmhKaFuQ46SccW8C2JDG/xA==
    =+VZi
    -----END PGP PUBLIC KEY BLOCK-----
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Carlos Castillo at Mar 5, 2013 at 5:56 pm
    To be more precise: although the net/http package supports automatic
    content-type detection (http://golang.org/pkg/net/http/#DetectContentType),
    which is run automatically if you don't specify a content type using
    http.ResponseWriter.Header(), it can only do so much. This automatic
    detection is not meant to be exhaustive or 100% correct; it is meant to
    cover the cases of serving complete files (with common filetypes), and
    server generated content in common patterns.

    What you provided to http.ResponseWriter.Write() looked like ASCII or UTF-8
    text, so it determined the content-type sent to the browser should be
    "text/plain" and not what you were expecting ("text/html"). When you send
    an actual full HTML document, the first few bytes will follow a pattern the
    detector can follow (eg: "<html>...", "<!DOCTYPE html>", etc...) and you
    will get the expected results. Since you were sending a HTML fragment, with
    no prefix it recognized as HTML, it decided the content was something else.

    To fix your example you can do one or both of the following:

    - Force the content-type header using the method specified by Dougx
    (NOTE: you must make your changes to the header before writing any data)
    - Ensure your HTML fragment looks like something the detector will treat
    as HTML (example: surround it with "<div>" or "<p>" tags)

    http://play.golang.org/p/LFM6d4Nmcf

    If you want to see how the automatic detection works:
    http://golang.org/src/pkg/net/http/sniff.go?s=648:690#L11
    On Monday, March 4, 2013 6:57:00 AM UTC-8, Dougx wrote:

    You must set the content type, or the browser will treat the output as
    text.

    Like this:

    func thing(w http.ResponseWriter, model map[string]interface{},
    w.Header().Add("Content-Type", "text/html") // <--- This
    fmt.Fprintf(w, "Bonjour <br>.... ")
    }

    ~
    Doug.
    On Monday, March 4, 2013 5:29:48 PM UTC+8, bussiere adrien wrote:


    I've made a little web server and one page but i doesn't understand why
    the output is not screened or printed as html :

    package main

    import (
    "fmt"
    "net/http"
    )

    func handler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintf(w, "Bonjour <br><a href='irc://10.0.0.1/42'>Serveur Irc sur
    port 667 ip : 10.0.0.1 channel principal #42</a><br><a href='sftp://
    pirate...@10.0.0.1'>Server de Partage de fichier en sftp sur le port 22
    avec id : pirate et mdp : pirate </a>")
    }

    func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":80", nil)
    }

    It writes me :

    Bonjour <br><a href='irc://10.0.0.1/42'>Serveur Irc sur port 667 ip : 10.0.0.1 channel principal #42</a><br><a href='sftp://pirate...@10.0.0.1'>Server de Partage de fichier en sftp sur le port 22 avec id : pirate et mdp : pirate </a>


    Regards

    Bussiere





    "Les nouvelles technologies offrent pleins de nouvelles possibilités,
    pleins de possibilités d'erreurs surtout en fait."
    insurance.aes256 : http://goo.gl/gHyAY
    -----BEGIN PGP PUBLIC KEY BLOCK-----
    Version: GnuPG v1.2.1 (MingW32) - WinPT 0.7.96rc1

    mQGiBE9sjdsRBADsY+DaDgj+pDuGLNVL6Dc+ghCxaEBK+9nwBvKPGIUocz13V42P
    5Jn/NJ9zRgZNdg5XbhohN2ltas8Nr09rrRQXbBk8zjCg/Qr0a4LmfA3j128SwCfZ
    aTe5zlo/WSSuGv6kWGe/KZrLmiQoZRTDbZzqWyZxuJRkcpgsZc+jYRXJHwCgualT
    LpC23Ja3plM6J6noFaqAlMUEAKZGDadSS/BW4gckw9XqSHRex7/XyWU4sqVQlhMy
    5DEIba7H8cU6grww+p/cYa3OCocv5Mycf6MbWDCPrtsG0X08u8PgEaE29BsLIgSm
    JF6r8y4Rgwj5xyCMTaxql82S+tZHyScpAoQ0a+xlTwxMZMuwK9p6b7oipXHL+UPN
    sDfkBACoYLS4PQPFotqWI96j83NNLwEumUzA6Rqyas8w6Xwr51quA65y/0nBmMmp
    l58tKN7eyc5+bIpRX5bGG6ktCUHnfzk9uvnG7peilWzYT6+AJ3gbBgMGLBPByJBN
    NH+nLjzsh4PzIxuSHLMiK5FkDNE2B95kaahIg42SQPT80LyvDLQlYnVzc2llcmVf
    cHJpdmF0ZSA8YnVzc2llcmVAZ21haWwuY29tPohZBBMRAgAZBQJPbI3bBAsHAwID
    FQIDAxYCAQIeAQIXgAAKCRAYpzZWoPP7b0h0AJ4n9feBF2FH4JUGhLU3XL3eIrOX
    9gCfZbN4JHYl/nRLL4uHrceRUk1M9Pm5Ac0ET2yN3hAHAK5MypLMT50yklt1n/pQ
    MjrFXuvM9IQvVyQemHMUBqKqxPTVMJLn86mR5mTLMqs+WVAqei8GTsQeSSvHyIbk
    8RvYrKY7aK2RVZUV1dHQ6PII0w3BI5C0L9GM/UyIq0VRXTW6zjp9hS/xgn5SrQad
    VIdGQKFVJa9h5/AzgjseARMQRkTQA/xX5/WmHxXaXUPhGxCeFsoqs53UFz1rkHYx
    mus2WiD0gHbJ58DiMlVmupeNumi4e9wa+mYuW4qGwjzsQn9ZFNdoiUFzQrqZa6o+
    Son+yZpGe/H9VidrWR5LhwD7AAMGBv9bhswvwpQ23X0kGItv7/jHVr/RJui7FAXY
    mOM4WAoUv/5ttPHf8OifWmeUDPlzm4txvR5a7t4hhVDehek1J7FEVvfG+ffcHiPP
    kkFaUv2EgrXziozt/Vkr2nngsKO5FpRJe9GSqn68uZirhwbkHSKX6LPNFTWSYR3E
    fefnsCaP82OJDBFGFMg2aMsz9QL85bg58fgHU972kLTePXUVAh3gaxb6qpjwZt2b
    yrCGJ/rnmIc/IWH8TJtHI5Yf3Abx2vlFTtzRH0CwVXuO0msjcfRrrrrS8NSm/ih5
    0ssElxKU6IhGBBgRAgAGBQJPbI3eAAoJEBinNlag8/tvoBEAn2Yqu0UFC5OSjD1Y
    JQ00FFjNVestAJ0XctcbmhKaFuQ46SccW8C2JDG/xA==
    =+VZi
    -----END PGP PUBLIC KEY BLOCK-----
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 4, '13 at 11:01a
activeMar 5, '13 at 5:56p
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase