如何解决如何在Golang中编码HTTP查询参数以进行重定向
我有一个golang链接重定向模块,该模块使用http服务器,获取请求并重定向 问题是处理查询字符串中的字符,我必须对其进行编码 虽然我可以在不进行任何编码的情况下重定向大多数字符,但http://之类的东西不起作用
我应该对此编码吗? 示例代码在这里
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/valyala/fasthttp"
)
func startHTTP(address string) {
s := &fasthttp.Server{
Handler: fastHTTPHandler,Name: "Custom HTTP",}
err := s.ListenAndServe(address)
if err != nil {
log.Fatalf("Could not Start http server at %s",address)
}
}
/* main function */
func main() {
startHTTP("127.0.0.1:9080")
}
func notFound(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx,"Helloworld NOT FOUND\n\n")
}
func handleRedirect(ctx *fasthttp.RequestCtx) {
//This link does not carry the original query string on redirection
link := "https://www.google.com?link=https://www.google.com/movie/2900"
ctx.Redirect(link,302)
ctx.SetStatusCode(302)
}
func fastHTTPHandler(ctx *fasthttp.RequestCtx) {
ctx.Response.Header.Set("Access-Control-Allow-Origin","*")
fmt.Printf("%v [%v] %v\n",time.Now().Format("2006-01-02 15:04:05.000000"),os.Getpid(),string(ctx.URI().RequestURI()))
switch string(ctx.Path()) {
case "/goredirect":
handleRedirect(ctx)
default:
notFound(ctx)
}
}
解决方法
如果要构建这样的URL,则必须转义查询字符串:
q,err:=url.QueryEscape("https://www.google.com/movie/2900")
link := "https://www.google.com?link="+q
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。