how to dynamically render as HTML/JSON/XML in golang with martini?



I am trying to have a simple API server site which serves HTML, JSON or XML format of the same date as requested by client. I am not able to figure out. I hope I am not doing something silly.


Code:



package main

import (
"http://ift.tt/1hFokBm"
"http://ift.tt/124iTaL"
)

type Ticket struct {
Number int `json:"number"`
Description string `json:"description"`
State string `json:"state"`
}

func dummyStatus() Ticket {

ticket := Ticket{
Number: 2345,
Description: "A dummy customer ticket",
State: "resolved",
}
return ticket
}

// http://localhost:3000/status/id:1
func ReadStatus(r render.Render, params martini.Params) Ticket {

return dummyStatus()
}

func WriteStatus(params martini.Params) Ticket {

return dummyStatus()
}

func main() {

m := martini.Classic()
m.Use(render.Renderer())

m.Group("/status", func(r martini.Router) {
r.Get("/:id", ReadStatus)
r.Post("/:id", WriteStatus)
})

m.Run()

}


Result: I request for JSON and I just receive a string



$ curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X GET http://localhost:3000/status/id:12345
HTTP/1.1 200 OK
Date: Wed, 24 Dec 2014 20:01:32 GMT
Content-Length: 19
Content-Type: text/plain; charset=utf-8

<main.Ticket Value>

No comments:

Post a Comment