Go言語 - こんなに簡単! Goで作るRESTサーバー - QiitaでRESTサーバーを作る方法を紹介したが、部分的にJSON以外のものを返したい場合があったので、その方法をしらべてみた。
main.go
package main
import (
"github.com/ant0ine/go-json-rest"
"log"
"net/http"
)
func index(w *rest.ResponseWriter, req *rest.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.Write([]byte("<div>html</div>"))
}
func main() {
handler := rest.ResourceHandler{}
handler.SetRoutes(
rest.Route{"GET", "/", index},
)
log.Printf("Server started")
http.ListenAndServe(":9999", &handler) // ポート9999で立ち上がる
}
HTTP/1.1 200 OK
Content-Length: 15
Content-Type: text/html; charset=utf-8
Date: Tue, 18 Feb 2014 18:02:42 GMT
<div>html</div>