dtz8044 2017-04-08 06:33
浏览 50
已采纳

如何在Golang中将请求封送至JSON

package controllers

import (
    "encoding/json"
    "errors"
    "io"
    "io/ioutil"
    "reflect"
)

func GetTypeFromReq(c *App, ty interface{}) (interface{}, error) {
    //get the type we are going to marshall into
    item := reflect.ValueOf(ty)

    //define and set the error that we will be returning to null
    var retErr error
    retErr = nil

    //extract the body from the request and defer closing of the body
    body, err := ioutil.ReadAll(io.LimitReader(c.Request.Body, 1048576))
    defer c.Request.Body.Close()

    //handle errors and unmarshal our data
    if err != nil {
        retErr = errors.New("Failed to Read body: " + err.Error())
    } else if err = json.Unmarshal(body, &item); err != nil {
        retErr = errors.New("Unmarshal Failed: " + err.Error())
    }

    return item, retErr
}

I am trying to pass a type and a request into a function, then inside that function unMarshall the request into a variable and return it.

I assume my approach is wrong because when i try to do this:

inter, err := GetTypeFromReq(&c, models.User{})
if err != nil {
    revel.ERROR.Println(err.Error())
}
user := inter.(models.User)

I get the error "interface conversion: interface {} is reflect.Value, not models.User"

any tips on how to approach this?

  • 写回答

3条回答 默认 最新

  • duanlangwen9597 2017-04-08 11:36
    关注

    Here's how to modify the the function to make it work as expected:

    func GetTypeFromReq(c *App, ty interface{}) (interface{}, error) {
      // Allocate new value with same type as ty
      v := reflect.New(reflect.TypeOf(ty))
    
      //define and set the error that we will be returning to null
      var retErr error
      retErr = nil
    
      //extract the body from the request and defer closing of the body
      body, err := ioutil.ReadAll(io.LimitReader(c.Request.Body, 1048576))
      defer c.Request.Body.Close()
    
      //handle errors and unmarshal our data
      if err != nil {
        retErr = errors.New("Failed to Read body: " + err.Error())
      } else if err = json.Unmarshal(body, v.Interface()); err != nil {
        retErr = errors.New("Unmarshal Failed: " + err.Error())
      }
    
      // v holds a pointer, call Elem() to get the value.
      return v.Elem().Interface(), retErr
    }
    

    Note the calls to Interface() to get a reflect.Value's current value.

    Here's an approach that avoids reflection and type assertions:

    func GetFromReq(c *App, item interface{}) error {
      //extract the body from the request and defer closing of the body
      body, err := ioutil.ReadAll(io.LimitReader(c.Request.Body, 1048576))
      defer c.Request.Body.Close()
    
      //handle errors and unmarshal our data
      if err != nil {
        retErr = errors.New("Failed to Read body: " + err.Error())
      } else if err = json.Unmarshal(body, item); err != nil {
        retErr = errors.New("Unmarshal Failed: " + err.Error())
      }
      return retErr
    }
    

    Use it like this:

    var user models.User
    err := GetFromReq(&c, &user)
    if err != nil {
       revel.ERROR.Println(err.Error())
    }
    

    Use a JSON decoder to simplify the code:

    func GetFromReq(c *App, item interface{}) error {
      defer c.Request.Body.Close()
      return json.NewDecoder(io.LimitReader(c.Request.Body, 1048576)).Deocode(item)
    }
    

    If c.Request is a *http.Request and c.Response is an http.ResponseWriter, then write the function as:

    func GetFromReq(c *App, item interface{}) error {
      return json.NewDecoder(http.MaxBytesReaer(c.Response, c.Request.Body, 1048576)).Deocode(item)
    }
    

    There's no need to close the request body in the net/http server. Use MaxBytesReader instead of io.LimitReader to prevents clients from accidentally or maliciously sending a large request and wasting server resources.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教