jjzjj

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

coder 2024-07-11 原文

我知道你可以从

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/

有关Go Lang- Gin : How to extract only the body (and ignore other garbage) from httputil. DumpRequest的更多相关文章

  1. 华为OD机试Golang解题 - 任务总执行时长 - 2

    华为Od必看系列华为OD机试全流程解析+经验分享,题型分享,防作弊指南)华为od机试,独家整理已参加机试人员的实战技巧华为od2023|什么是华为od,od薪资待遇,od机试题清单华为OD机试真题大全,用Python解华为机试题|机试宝典文章目录华为Od必看系列使用说明本期题目:任务总执行时长题目输入输出示例一输入输出说明go代码实现华为OD其它语言版本

  2. 【编程实践】Golang 获取HTTP请求的IP地址 - 2

    目录Golang获取HTTP请求的IP地址HTTP的发展历史3,HTTP所在的网络层次4,HTTP请求与响应

  3. Golang SQL 查询语法 - 2

    关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭4年前。Improvethisquestion在golang代码中使用sql查询获取语法错误。golang中此SQL查询所需的正确语法:rows,errQuery:=dbCon.Query("SELECTB.LatestDate,A.SVRNameASServerName,A.DRIVE,A.Tot

  4. go - 在golang中的表达式中检查括号是否平衡[保持] - 2

    给定表达式字符串exp,编写程序检查exp中“{”、“}”、“(”、“)”、“[”、“]的对和顺序是否正确。packagemainimport("fmt"stack"github.com/golang-collections/collections/stack")funcmain(){s:="(a[0]+b[2c[6]])){24+53}"stackO:=stack.New()stackmap:=map[string]string{"[":"]","(":")","{":"}"}varstr=""for_,num:=ranges{str=string(num)if(str=="{"||

  5. Golang libphonenumber - 2

    关闭。这个问题是notreproducibleorwascausedbytypos.它目前不接受答案。这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topic在这里,这个问题的解决方式不太可能帮助future的读者。关闭3年前。Improvethisquestion如何通过使用此库在golang中传递数字来获取国家代码:https://godoc.org/github.com/nyaruka/phonenumbers?

  6. go - func 的语法 Golang 错误 - 2

    为什么下面的代码会抛出意外的函数错误?我看到错误./func_correct.go:4:syntaxerror:unexpectedfunc,expectingnamepackagemainfunc(st*Stack)Pop()int{v:=0forix:=len(st)-1;ix>=0;ix--{ifv=st[ix];v!=0{st[ix]=0returnv}}return0}funcmain(){Pop()} 最佳答案 定义堆栈类型在main中为其创建一个变量对其调用Pop代码:packagemainimport"fmt"typ

  7. go - 进程花费了太长时间程序退出:Golang错误 - 2

    Thisquestionalreadyhasanswershere:ForloopoftwovariablesinGo(3个答案)2年前关闭。我正在通过Gotour在Go中使用for循环我跑的时候packagemainimport"fmt"funcmain(){sum:=1forsum程序运行正常,输出为1024但是当我更改sum:=0时packagemainimport"fmt"funcmain(){sum:=0forsum它给出了错误的说法processtooktoolongProgramexited.编辑:我沉迷于Go巡回赛,以至于我无法意识到,我犯了一个逻辑错误:P。

  8. json - 如何在golang中将结构解码到 map 中 - 2

    关闭。这个问题需要debuggingdetails.它目前不接受答案。编辑问题以包含desiredbehavior,aspecificproblemorerror,andtheshortestcodenecessarytoreproducetheproblem.这将有助于其他人回答问题。关闭4年前。Improvethisquestion我有一个Json:{"id":"me","name":"myname","planets":{"EARTH":3,"MARS":4}}我不知道如何将planets字段解码为map[string]int,所以我将访问元素而无需解码它们,就像在这个例子中一样

  9. mysql - golang MySQL "connection refused" - 2

    我是Go(Golang)的新手。我写了一个简单的基准程序来测试MySQL的并发处理。当我增加并发channel数时,不断收到“dialtcp52.55.254.165:3306:getsockopt:connectionrefused”、“unexpectedEOF”错误。每个go例程都将1到n行批量插入到一个简单的客户表中。该程序允许设置可变插入大小(单个语句中的行数)和并行go例程的数量(每个go例程执行上面的一个插入)。程序在小数字row寻找线索。基于它们,我设置了数据库最大连接数以及“max_allowed_pa​​cket”和“max_connections”。我还设置了go

  10. go - Golang "func (t *SomeType) myFuncName(param1, param2)"语法是什么意思 - 2

    这个问题在这里已经有了答案:Functiondeclarationsyntax:thingsinparenthesisbeforefunctionname(3个答案)WhatsthedifferenceoffunctionsandmethodsinGo?(5个答案)ParameterbeforethefunctionnameinGo?[duplicate](1个回答)WhatdothebracketsafterfuncmeaninGo?[duplicate](1个回答)关闭8个月前。我正在学习Golang-在教程中我经常看到这样的语法:typeSomeTypestruct{//stru

随机推荐