gpt4 book ai didi

Go Lang- Gin : How to extract only the body (and ignore other garbage) from httputil. DumpRequest

转载 作者:数据小太阳 更新时间:2023-10-29 03:33:23 27 4
gpt4 key购买 nike

我知道你可以从

ioutil.ReadAll(c.Request.Body)

但是使用 httputil.DumpRequest

转储,错误 := httputil.DumpRequest(c.Request, true)

将给出正文内容以及其他值,最后是正文内容。

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"}

有没有一种有效的方法可以只从 httputil.DumpRequest() 获取正文内容?即仅在这种情况下

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

最佳答案

您不为此使用 httputil.DumpRequest,这是一个调试功能。

假设你想解析 json,你可以这样做:

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

关于Go Lang- Gin : How to extract only the body (and ignore other garbage) from httputil. DumpRequest,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32034673/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com