通过文件访问通过POST接收到的JSON数据。

huangapple go评论80阅读模式
英文:

Accessing JSON received via POST from file

问题

我正在使用Martini构建一个RESTful API,但是在通过curl命令将book.json发送到我的服务时,我很难访问book.json的内容。

book.json不是一个二进制文件,而是一个包含JSON数组的简单文本文件。我该如何访问传输的JSON数据?http.Request的PostForm为空。

英文:

I'm building a RESTful API using Martini and have a hard time accessing the contents of book.json sent to my service via

curl -X POST "http://localhost:8080/books" -H "Content-Type: application/json" -d @book.json

book.json is not a binary file but a simple text file containing a JSON array. How can I access the transmitted JSON? PostForm on the http.Request is empty.

答案1

得分: 1

我知道这是旧的,但你可能在寻找 Martini Binding。

https://github.com/martini-contrib/binding

m.Post("/contact/submit", binding.Bind(ContactForm{}), func(contact ContactForm) string {
    return fmt.Sprintf("Name: %s\nEmail: %s\nMessage: %s",
        contact.Name, contact.Email, contact.Message)
})
英文:

I know this is old but you are probably looking for Martini Binding

https://github.com/martini-contrib/binding

m.Post("/contact/submit", binding.Bind(ContactForm{}), func(contact ContactForm) string {
    return fmt.Sprintf("Name: %s\nEmail: %s\nMessage: %s",
        contact.Name, contact.Email, contact.Message)
})

答案2

得分: 0

你可能有一个可以解析的请求体数据。我认为这篇文章很好地解释了这个问题:http://nathanleclaire.com/blog/2013/11/30/fear-and-loathing-with-golang-and-angular-js/

英文:

You probably have data in request.Body that you can demarshal.
Imho this aticle explains well the problem: http://nathanleclaire.com/blog/2013/11/30/fear-and-loathing-with-golang-and-angular-js/

huangapple
  • 本文由 发表于 2014年3月18日 17:13:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/22474784.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定