代码之家  ›  专栏  ›  技术社区  ›  Alvaro Denis Acosta

我无法在revel go框架中发布尸体

  •  2
  • Alvaro Denis Acosta  · 技术社区  · 8 年前

    我正在尝试使用revel中的Rest架构实现一个基本的CRUD,但我无法发送在中编码的数据 json 格式化到端点,我尝试了多种方法来检查请求中的正文内容,因此现在我有一个“最小可编译示例”:

    1. 使用revel cli工具创建新项目。

    2. 应用以下更改

      diff --git a/app/controllers/app.go b/app/controllers/app.go
      index 1e94062..651dbec 100644
      --- a/app/controllers/app.go
      +++ b/app/controllers/app.go
      @@ -9,5 +9,6 @@ type App struct {
       }
      
       func (c App) Index() revel.Result {
      -   return c.Render()
      +   defer c.Request.Body.Close()
      +   return c.RenderJSON(c.Request.Body)
       }
      diff --git a/conf/routes b/conf/routes
      index 35e99fa..5d6d1d6 100644
      --- a/conf/routes
      +++ b/conf/routes
      @@ -7,7 +7,7 @@ module:testrunner
       # module:jobs
      
      
      -GET     /                                       App.Index
      +POST     /                                       App.Index
      
       # Ignore favicon requests
       GET     /favicon.ico                            404
      
    3. curl --request POST --header "Content-Type: application/json" --header "Accept: application/json" --data '{"name": "Revel framework"}' http://localhost:9000
      

    我的问题;curl调用不会给我回音(相同 json {"name": "Revel framework"}

    https://github.com/revel/revel/issues/126

    1 回复  |  直到 8 年前
        1
  •  5
  •   Jonathan Hall    8 年前

    source of Revel application/json text/json c.Params.JSON []byte .

    Request.Body c.Request.Body 无法使用正确序列化 c.RenderJSON()

    Revel具有方便的功能 Params.BindJSON 其中转换 c.Params.JSON

    type MyData struct {
        Name string `json:"name"`
    }
    
    func (c App) Index() revel.Result {
        data := MyData{}
        c.Params.BindJSON(&data)
        return c.RenderJSON(data)
    }
    
    推荐文章