Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/go/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Http 如何在golang中为请求和重定向添加值_Http_Go_Request_Microservices - Fatal编程技术网

Http 如何在golang中为请求和重定向添加值

Http 如何在golang中为请求和重定向添加值,http,go,request,microservices,Http,Go,Request,Microservices,我有API网关和用户服务微服务。当一个请求到达API网关时,我需要在Rediret()it到用户服务之前添加一些值 func main(){ http.HandleFunc("/login", userLogin) } func userLogin(res http.ResponseWriter, req *http.Request) { uuid := generateUUID() // How to add UID to request? http.Re

我有API网关和用户服务微服务。当一个请求到达API网关时,我需要在
Rediret()
it到用户服务之前添加一些值

func main(){
    http.HandleFunc("/login", userLogin)

}
func userLogin(res http.ResponseWriter, req *http.Request) {

    uuid := generateUUID()

    // How to add UID to request?
    http.Redirect(res, req, userservice, http.StatusSeeOther)

}
为此,我使用了描述的方法

这只需转到用户服务的登录路径并停止即可

我还尝试使用:
req.Form.Set(“uid”、“foo”)

这引起了恐慌

http:127.0.0.1:55076:分配到nil映射中的条目

我的用户服务:

func main(){
    http.HandleFunc("/login", UserLogin)
}

func UserLogin(res http.ResponseWriter, req *http.Request) {
     req.ParseForm()
     requestID := req.FormValue("uid")
     userID := req.FormValue("userid")

     if userID =="sachith"{
        sendRequest(requestID)
        http.Redirect(res, req, "http://localhost:7070/home", http.StatusSeeOther)
    }

是否有方法向我们接收的路由请求添加值并将其重定向到另一个服务?

要将查询参数添加到重定向位置,只需将它们添加到函数的
url
参数中即可

另外,您不应该修改传递到
重定向
*http.Request
参数来指定目标位置,这不是它的用途。首先,修改它只会对相对重定向产生影响,这不是您在这里尝试的。第二,
url
是为此目的而指定的,可以一致地用于执行相对和绝对重定向。在这里违背设计没有任何好处

apigateway/main.go

package main

import (
    "net/http"
)

func main() {
    http.HandleFunc("/login", loginHandler)
    http.ListenAndServe(":8080", nil)
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
    uuid := "911cf95b-6b3f-43fd-b695-c80c79145c51" // generate uuid
    http.Redirect(w, r, "http://localhost:8081/login?user_id="+uuid, http.StatusSeeOther)
}
package main

import (
    "net/http"
)

func main() {
    http.HandleFunc("/login", loginHandler)
    http.ListenAndServe(":8081", nil)
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
    uuid := r.URL.Query().Get("user_id")
    w.Write([]byte(uuid))
}
userservice/main.go

package main

import (
    "net/http"
)

func main() {
    http.HandleFunc("/login", loginHandler)
    http.ListenAndServe(":8080", nil)
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
    uuid := "911cf95b-6b3f-43fd-b695-c80c79145c51" // generate uuid
    http.Redirect(w, r, "http://localhost:8081/login?user_id="+uuid, http.StatusSeeOther)
}
package main

import (
    "net/http"
)

func main() {
    http.HandleFunc("/login", loginHandler)
    http.ListenAndServe(":8081", nil)
}

func loginHandler(w http.ResponseWriter, r *http.Request) {
    uuid := r.URL.Query().Get("user_id")
    w.Write([]byte(uuid))
}

您是否尝试过将查询参数添加到
Redirect
方法的
url
参数中?(当然,正确格式化为
&
)。例如,
http.Redirect(w,r,userservice+“?uid=far”,http.StatusSeeOther)
。将这些查询参数添加到req并不像您期望的那样。重定向重定向到您提供的url作为3。arguemnt,您可能想在那里添加查询参数。请注意,链接帖子中的解决方案是针对客户端请求的,您在这里处理的是一个服务器请求,一旦收到,应该很少(如果有的话),被非中间件处理程序修改。@SachithMuhandiram帮助我们帮助您。@SachithMuhandiram我不知道
/login/{uid}
应该如何帮助您。如果您的带有查询参数的简单重定向(如前一条评论中所建议的)不适用于您,那么执行
/login/{uid}
不可能解决实际问题。重定向,无论是否使用查询参数,都非常简单,例如,请参见以下要点:它在我的本地计算机上工作,如果无法在您的环境中运行此示例,则您的环境将被破坏。Go-net/http.Redirect的工作原理与文档中所宣传的一样。这也解决了我的另外两个问题。我错过了请求/响应周期。我稍后会更新这个问题。