duanke1984 2015-08-16 11:40
浏览 959

Go Lang-Gin:如何从httputil.DumpRequest中仅提取主体(并忽略其他垃圾)

I know that you can get the body content from

ioutil.ReadAll(c.Request.Body)

But using httputil.DumpRequest

dump, err := httputil.DumpRequest(c.Request, true)

will give the body contents along with other values, the body content in the end.

Content type: application/json IP: 127.0.0.1:36846 header token: Content length: 76 Request Method: POST Request URL: /signup Body: POST /signup HTTP/1.1 Host: 127.0.0.1:8080 Accept: / Accept-Encoding: gzip,deflate Accept-Language: en-US,en;q=0.8 Connection: keep-alive Content-Type: application/json Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36 {"fname":"aFirstName", "lname":"aLName", "email":"test@test.com", "password":"123"}

Is there an efficient way I can get only the body content from httputil.DumpRequest()? i.e In this case only

{"fname":"aFirstName", "lname":"aLName", "email":"test@test.com", "password":"123"}

  • 写回答

1条回答 默认 最新

  • dongyun6003 2015-08-16 20:22
    关注

    You don't use httputil.DumpRequest for that, that's a debug function.

    Assuming you want to parse json you can do something like this:

    defer c.Request.Body.Close()
    var data yourDataType
    if err := json.NewDecoder(c.Request.Body).Decode(&data); err != nil {
        // handle error
    }
    // handle data
    
    评论

报告相同问题?

悬赏问题

  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误