英文:
how to dynamically render as HTML/JSON/XML in golang with martini?
问题
我正在尝试在golang网站中创建一个简单的REST API服务器,该服务器根据客户端的请求以HTML、JSON或XML格式返回相同的日期。我无法弄清楚问题所在。希望我没有做什么愚蠢的事情。
代码:
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
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 {
// read from DB
return dummyStatus()
}
func WriteStatus(params martini.Params) Ticket {
// write to DB
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()
}
结果:
我请求JSON格式,但只收到一个字符串。
$ 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>
英文:
I am trying to have a simple REST API server in golang 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 (
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
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 {
// read from DB
return dummyStatus()
}
func WriteStatus(params martini.Params) Ticket {
// write to DB
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>
答案1
得分: 2
通过一些试错,我找到了解决方法,但是我仍然在努力使其与路由组一起工作。如果我找到了解决方法,我会更新这个答案。希望对你有帮助。
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
type Ticket struct {
Number int `json:"number"`
Description string `json:"description"`
State string `json:"state"`
}
func ReadStatus(p martini.Params) Ticket {
ticket := Ticket{
Number: 645,
Description: "A dummy customer ticket " + p["id"],
State: "resolved",
}
return ticket
}
func main() {
m := martini.Classic()
m.Use(render.Renderer())
m.Get("/status/:id", func(r render.Render, params martini.Params) { r.JSON(200, ReadStatus(params)) })
m.Run()
}
英文:
With some trial and error, I figured this out, however, I am still struggling to make it work with routing group. If I ever figure that out, I will update this answer. Hope this helps.
package main
import (
"github.com/go-martini/martini"
"github.com/martini-contrib/render"
)
type Ticket struct {
Number int `json:"number"`
Description string `json:"description"`
State string `json:"state"`
}
func ReadStatus(p martini.Params) Ticket {
ticket := Ticket{
Number: 645,
Description: "A dummy customer ticket " + p["id"],
State: "resolved",
}
return ticket
}
func main() {
m := martini.Classic()
m.Use(render.Renderer())
m.Get("/status/:id", func(r render.Render, params martini.Params) { r.JSON(200, ReadStatus(params)) })
m.Run()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论