duanniling0018 2017-11-08 18:07
浏览 153
已采纳

如何从Go上的请求主体获取JSON

I'm a newbie with Go, but so far I'm liking it very much.

I have a problem I can't figure out. I'm migrating an API from Node to Go and there is this log where I have to capture the Body of a POST AS IT IS and save it to a jsonb type column in a Postgresql database.

This means I can't use a struct or anything predetermined.

The POST is made with body raw Content-Type: application/json like so:

{
    "debug": false,
    "order_id_gea": 326064,
    "increment_id_gea": 200436102,
    "date": "2017-05-18T01:44:44+00:00",
    "total_amount": 10000.00,
    "currency": "MXN",
    "payment_method": "Referencia bancaria",
    "reference": "857374",
    "buyer": {
        "buyer_id_gea": 1234,
        "full_name": "Juan Perez Martinez",
        "email": "juanperez@gmail.com",
        "phone": "5512341234"
    },
    "products": [
        {
            "sku":"PEP16114",
            "price": 10000.00,
            "currency": "MXN",
            "student": {
                "school_id_gea": 172,
                "grade_id_gea": 119,
                "level_id_gea": 36,
                "name": "Benancio",
                "last_name": "Perez",
                "second_last_name": "Garcia",
                "email": "benancio@gmail.com"
            }
        }
    ]
}

On Node + Hapi is quite simple:

const payload = request.payload

and then I can access the JSON from payload.

I am using Go with Echo, so context is a wrapper where I can find Request() *http.Request.

I have tried the following, but everytime the result is empty or an error because it is empty:

var v interface{}
err := json.NewDecoder(context.Request().Body).Decode(&v)
if err != nil {
    return result, err
}
fmt.Println(v)

Result: EOF

--

m := echo.Map{}
if err := context.Bind(&m); err != nil {
    return result, err
}
fmt.Println(m)

Result code 400, message EOF

--

body, error := ioutil.ReadAll(context.Request().Body)
if error != nil {
    return result, error
}
fmt.Println(body)

Result []

--

What am I missing and/or doing wrong? Thanks!

  • 写回答

2条回答 默认 最新

  • donglusou3335 2017-11-14 21:41
    关注

    TIL that http.Response.Body is a buffer, which means that once it has been read, it cannot be read again.

    It's like a stream of water, you can see it and measure it as it passes but once it's gone, it's gone.

    However, knowing this, there is a workaround, you need to "catch" the body and restore it:

    // Read the Body content
    var bodyBytes []byte
    if context.Request().Body != nil {
        bodyBytes, _ = ioutil.ReadAll(context.Request().Body)
    }
    
    // Restore the io.ReadCloser to its original state
    context.Request().Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
    
    // Continue to use the Body, like Binding it to a struct:
    order := new(models.GeaOrder)
    error := context.Bind(order)
    

    Now, you can use context.Request().Body somewhere else.

    Sources:

    http://grokbase.com/t/gg/golang-nuts/12adq8a2ys/go-nuts-re-reading-http-response-body-or-any-reader

    https://medium.com/@xoen/golang-read-from-an-io-readwriter-without-loosing-its-content-2c6911805361

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog