在 Go-chi 中间件处理程序中重用 HTTP 请求体
当使用 go-chi 进行 HTTP 路由时,可以方便地在多个中重用代码处理程序。但是,如果处理程序依赖于请求正文数据,这可能会导致意外问题。
考虑以下场景:
func Registration(w http.ResponseWriter, r *http.Request) { b, err := ioutil.ReadAll(r.Body) // if you delete this line, the user will be created // ...other code // if all good then create new user user.Create(w, r) } ... func Create(w http.ResponseWriter, r *http.Request) { b, err := ioutil.ReadAll(r.Body) // ...other code // ... there I get the problem with parse JSON from &b }
在此示例中,注册和创建处理程序都尝试读取使用 ioutil.ReadAll 的请求正文。但是,由于 Registration 会读取正文到末尾,因此在调用 Create 时没有剩余数据可供读取。
要解决此问题,外部处理程序(Registration)必须使用之前读取的数据恢复请求正文。这可以使用以下代码来实现:
func Registration(w http.ResponseWriter, r *http.Request) { b, err := io.ReadAll(r.Body) // ...other code r.Body = io.NopCloser(bytes.NewReader(b)) user.Create(w, r) }
这里,bytes.NewReader() 函数在字节切片上返回 io.Reader,而 io.NopCloser 函数将此读取器转换为 io.Reader。 r.Body 需要 ReadCloser 接口。通过使用原始数据重置 r.Body,Create 现在可以按预期访问和解析请求正文。
以上是如何跨 Go-chi 中间件处理程序重用 HTTP 请求正文数据?的详细内容。更多信息请关注PHP中文网其他相关文章!

theEncoding/binarypackageingoisessential forhandlingbinarydata,offeringFunctionStoreadAndWritedAtainBig-Endianandlittle-endianFormats.1)IT'SidealFornetwork-work-workprotocels,enableSeringSeringSerializationalializationalialization andDeSerialization andDeSerializationOfStructuredDatalizedDataliakePackackEtheadErloth

theessentionfunctionsingo'sbytespackageThatyOuneedToknoware:1)字节.indexforsearchingwithinbyteslices,2)bytes.splitforparsing数据,3)字节。joinforConcatenatingslices,4)bytes.containsforcheckingsubslicepresence和5)bytes.replaceallfordatatatatransformatio

Gooffersalternativestothestringspackageforstringmanipulation:1)Theregexppackageforcomplexpatternmatching,2)Thestrconvpackagefornumericconversions,and3)Externallibrarieslikestrutilforspecializedoperations.Theseoptionscatertodifferentneeds,enhancingyou

效率地使用/binarypackageforhandlingvariousdatatypes,lofterTheSesteps:1)指定bytedorder(例如,binary.littleendian)for -compatibility.2)Usepututuint32/uint322222222222forintegerSandfloat32bits/floatt32bits/floatth322222222frollombitsss.3222frollombitss.3)

掌握bytes包的原因是它能显着提高处理字节切片的效率和性能。 1)bytes包提供了强大的工具,如bytes.Contains用于搜索字节序列,2)bytes.Buffer类型适用于增量构建字节切片,3)了解bytes包的使用陷阱和性能优化策略,如重用bytes.Buffer实例,可以避免常见错误并提升效率。

Go的"strings"包提供了丰富的功能,使字符串操作高效且简单。1)使用strings.Contains()检查子串。2)strings.Split()可用于解析数据,但需谨慎使用以避免性能问题。3)strings.Join()适用于格式化字符串,但对小数据集,循环使用 =更有效。4)对于大字符串,使用strings.Builder构建字符串更高效。

Go语言使用"strings"包进行字符串操作。1)拼接字符串使用strings.Join函数。2)查找子串使用strings.Contains函数。3)替换字符串使用strings.Replace函数,这些函数高效且易用,适用于各种字符串处理任务。

资助bytespackageingoisesential foreffited byteSemanipulation,uperingFunctionsLikeContains,index,andReplaceForsearchingangingAndModifyingBinaryData.itenHancesperformanceNandCoderAceAnibility,MakeitiTavitalToolToolToolToolToolToolToolToolToolForhandLingBinaryData,networkProtocols,networkProtocoLss,networkProtocols,andetFilei


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

记事本++7.3.1
好用且免费的代码编辑器

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境