【问题标题】:Accessing JSON received via POST from file访问通过 POST 从文件接收的 JSON
【发布时间】:2014-04-23 20:43:26
【问题描述】:

我正在使用 Martini 构建一个 RESTful API,并且很难访问通过

发送到我的服务的 book.json 的内容
curl -X POST "http://localhost:8080/books" -H "Content-Type: application/json" -d @book.json

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

【问题讨论】:

  • 你能展示你的处理程序处理 POST 请求吗?
  • 您在访问PostForm 之前是否致电request.ParseForm()?请显示您的 go 处理程序代码和数据结构和 json,因为它们都是查明问题所必需的!
  • 你可以试试book:=Book{};json.NewDecoder(req.Body).Decode(&book)吗?
  • 或者你想要的只是打印出来?也许ioutil.ReadAll(req.Body)
  • 请出示 /books 的处理程序

标签: go martini


【解决方案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)
})

【讨论】:

    【解决方案2】:

    您可能在 request.Body 中有可以解组的数据。 恕我直言,这篇文章很好地解释了这个问题:http://nathanleclaire.com/blog/2013/11/30/fear-and-loathing-with-golang-and-angular-js/

    【讨论】:

      猜你喜欢
      • 2021-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多