package main
import (
"net/http"
)
func helloWorld(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
switch r.Header.Get("Accept") {
case "application/json":
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write([]byte(`{"message": "Hello World"}`))
case "application/xml":
w.Header().Set("Content-Type", "application/xml; charset=utf-8")
w.Write([]byte(`<?xml version="1.0" encoding="utf-8"?><Message>Hello World</Message>`)
default:
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Write([]byte("Hello World\n"))
}
}
func main() {
http.HandleFunc("/", helloWorld)
http.ListenAndServe(":8000", nil)
}
curl -si -H 'Accept: application/json' http://localhost:8000
package main
import (
"net/http"
)
func helloWorld(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
switch r.Method {
case "GET":
w.Write([]byte("Received a GET request\n"))
case "POST":
w.Write([]byte("Received a POST request\n"))
default:
w.WriteHeader(http.StatusNotImplemented)
w.Write([]byte(http.StatusText(http.StatusNotImplemented)) + "\n")
}
}
func main() {
http.HandleFunc("/", helloWorld)
http.ListenAndServe(":8000", nil)
}
curl -si -X GET http://localhost:8000
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func helloWorld(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
switch r.Method {
case "GET":
for k, v := range r.URL.Query() {
fmt.Printf("%s: %s\n", k, v)
}
w.Write([]byte("Received a GET request\n"))
case "POST":
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", reqBody)
w.Write([]byte("Received a POST request\n"))
default:
w.WriteHeader(http.StatusNotImplemented)
w.Write([]byte(http.StatusText(http.StatusNotImplemented)))
}
}
func main() {
http.HandleFunc("/", helloWorld)
http.ListenAndServe(":8000", nil)
}
curl -si -X POST -d "test" http://localhost:8000/
'Language > go' 카테고리의 다른 글
json 코드 (0) | 2018.03.15 |
---|---|
http client 코드 (0) | 2018.03.15 |
goroutine 기본 구조 (0) | 2018.01.02 |
go error handling (0) | 2018.01.02 |
go type default 값 (0) | 2018.01.01 |