英文:
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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论