重定向POST请求带有正文数据的GoLang操作
来源:stackoverflow
时间:2024-02-24 10:12:25 367浏览 收藏
偷偷努力,悄无声息地变强,然后惊艳所有人!哈哈,小伙伴们又来学习啦~今天我将给大家介绍《重定向POST请求带有正文数据的GoLang操作》,这篇文章主要会讲到等等知识点,不知道大家对其都有多少了解,下面我们就一起来看一吧!当然,非常希望大家能多多评论,给出合理的建议,我们一起学习,一起进步!
我想针对 post 请求使用相同的正文进行重定向。
来自 go 来源 (client.go)
func redirectbehavior(reqmethod string, resp *response, ireq *request) (redirectmethod string, shouldredirect, includebody bool) { switch resp.statuscode { case 301, 302, 303: redirectmethod = reqmethod shouldredirect = true includebody = false // rfc 2616 allowed automatic redirection only with get and // head requests. rfc 7231 lifts this restriction, but we still // restrict other methods to get to maintain compatibility. // see issue 18570.
但有时服务器会为 post 请求返回 302 redirect,这意味着我需要将相同的正文发送到另一个位置。
遇到这种情况我该怎么办?
func FollowRedirectForPost() { client := &http.Client{} req, _ := http.NewRequest(http.MethodPost, "example.com/test", strings.NewReader(url.Values{ "key": {"value"}, "key1":{"value1"}, }.Encode())) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") client.Do(req) // If server returns 302 Redirect so it means I need make the same request with the same body to // a different location. Just imagine i replaced "example.com/test" to "example.com/redirect_url" }
正确答案
来自 RFC7231:
服务器应该在响应中生成 location 标头字段 包含不同 uri 的 uri 引用。用户代理可以 使用位置字段值进行自动重定向。服务器的 响应负载通常包含一个简短的超文本注释,其中包含 指向不同 uri 的超链接。
注意:由于历史原因,用户代理可能会更改请求 后续请求的方法从 post 到 get。如果这 行为是不受欢迎的,307(临时重定向)状态代码 可以用它代替。
因此您可以遵循重定向,新的 uri 位于 location
标头中。你不必这样做。您可以将方法更改为 get,但不是必须这样做。因此本质上,您所做的任何事情都符合 rfc。
您可以通过提供 checkredirect
函数来提供自己的重定向策略。如果 redirectposton302
基本上与 client would do 相同,如果 includebody
是 true
并且 redirectmethod
是 post
:
func followredirectforpost() { client := &http.client{ checkredirect: redirectposton302, } req, _ := http.newrequest(http.methodpost, "example.com/test", strings.newreader(url.values{ "key": {"value"}, "key1":{"value1"}, }.encode())) req.header.add("content-type", "application/x-www-form-urlencoded") client.do(req) // if server returns 302 redirect so it means i need make the same request with the same body to // a different location. just imagine i replaced "example.com/test" to "example.com/redirect_url" } func redirectposton302(req *http.request, via []*http.request) error { if len(via) >= 10 { return errors.new("stopped after 10 redirects") } lastreq := via[len(via)-1] if req.response.statuscode == 302 && lastreq.method == http.methodpost { req.method = http.methodpost // get the body of the original request, set here, since req.body will be nil if a 302 was returned if via[0].getbody != nil { var err error req.body, err = via[0].getbody() if err != nil { return err } req.contentlength = via[0].contentlength } } return nil }
最佳做法是更改服务器状态代码 - 307(临时重定向)或 308(永久重定向)。
另一种超级黑客方法可能是 - 更改 checkredirect 函数中的请求 https://github.com/golang/go/blob/master/src/net/http/client.go#L78 https://github.com/golang/go/blob/master/src/net/http/client.go#L691
// example hack func FollowRedirectForPost(data io.Reader) { client := &http.Client{ CheckRedirect: func(req *Request, via []*Request) error { // check status code etc. req.Method = http.MethodPost req.Body = data } } req, _ := http.NewRequest(http.MethodPost, "example.com/test", data) req.Header.Add("Content-Type", "application/x-www-form-urlencoded") client.Do(req) // If server returns 302 Redirect so it means I need make the same request with the same body to // a different location. Just imagine i replaced "example.com/test" to "example.com/redirect_url" }
好了,本文到此结束,带大家了解了《重定向POST请求带有正文数据的GoLang操作》,希望本文对你有所帮助!关注golang学习网公众号,给大家分享更多Golang知识!
-
502 收藏
-
502 收藏
-
501 收藏
-
501 收藏
-
501 收藏
-
139 收藏
-
204 收藏
-
325 收藏
-
477 收藏
-
486 收藏
-
439 收藏
-
357 收藏
-
352 收藏
-
101 收藏
-
440 收藏
-
212 收藏
-
143 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 立即学习 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 立即学习 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 立即学习 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 立即学习 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 立即学习 484次学习