Created
February 18, 2014 07:46
-
-
Save jnriver/9066336 to your computer and use it in GitHub Desktop.
Go set http request header
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"net/http" | |
) | |
func main() { | |
client := &http.Client{} | |
reqest, _ := http.NewRequest("GET", "http://localhost:9090", nil) | |
reqest.Header.Set("Auth-Token", "123") | |
client.Do(reqest) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
path: / | |
method: GET | |
header: map[User-Agent:[Go 1.1 package http] Auth-Token:[123] Accept-Encoding:[gzip]] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
) | |
func sayHello(w http.ResponseWriter, r *http.Request) { | |
fmt.Println("") | |
fmt.Println("path:", r.URL.Path) | |
fmt.Println("method:", r.Method) | |
fmt.Println("header:", r.Header) | |
fmt.Fprintf(w, "Hello world!") //这个写入到w的是输出到客户端的 | |
} | |
func main() { | |
http.HandleFunc("/", sayHello) //设置访问的路由 | |
http.HandleFunc("/login", login) //设置访问的路由 | |
err := http.ListenAndServe(":9090", nil) //设置监听的端口 | |
if err != nil { | |
log.Fatal("ListenAndServe: ", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment