LoginSignup
6
5

More than 5 years have passed since last update.

Go言語: go-json-restでJSONではなくHTMLを返す

Posted at

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>
6
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
5