在Go中获取URL参数的问题

huangapple go评论86阅读模式
英文:

Getting the URL parameter in Go issue

问题

我有一个类似这样的URL:http://localhost/templates/verify?key=ijio

我的路由器代码如下:

import (
    "github.com/gorilla/mux"
    "github.com/justinas/alice"
)

ctx := &model.AppContext{db, cfg} // 传入数据库和配置
verifyUser := controller.Verify(ctx)
mx.Handle("/verify", commonHandlers.ThenFunc(verifyUser)).Methods("GET").Name("verify")

我想从URL中获取key参数,所以我使用了以下代码:

func Verify(c *model.AppContext) http.HandlerFunc {
    fn := func(w http.ResponseWriter, r *http.Request) {

        key := r.URL.Query().Get("key") // 获取URL中的哈希值
        log.Println(key) // 空的key
        log.Println(r.URL.Query()) // 返回map[]
        // 处理key并返回JSON响应的代码
    }
}

我使用AngularJS获取JSON数据:

app.controller("verifyControl", ['$scope', '$http', function($scope, $http) {
    $scope.message = "";
    $http({
        method: 'GET',
        url: "/verify"
    }).success(function(data) {
        $scope.message = data.msg; // JSON响应
    });

}]);

然而,当我尝试打印key变量时,它为空。最近我使用nginx去掉了.html扩展名,如果可能的话,这可能是问题的原因。我该如何解决这个问题?

英文:

I have a URL that looks something like this: http://localhost/templates/verify?key=ijio

My router looks like this:

import (
"github.com/gorilla/mux"
"github.com/justinas/alice"
)

ctx := &model.AppContext{db, cfg} // passes in database and config
verifyUser := controller.Verify(ctx)
mx.Handle("/verify", commonHandlers.ThenFunc(verifyUser)).Methods("GET").Name("verify")

I want to get the key parameter out of the URL, so I use the following code:

func Verify(c *model.AppContext) http.HandlerFunc {
	fn := func(w http.ResponseWriter, r *http.Request) {

    key := r.URL.Query().Get("key") // gets the hash value that was placed in the URL
    log.Println(key) // empty key
    log.Println(r.URL.Query()) // returns map[]
    // code that does something with key and sends back JSON response
   }
}

I used AngularJS to get the JSON data:

app.controller("verifyControl", ['$scope', '$http', function($scope, $http) {
    $scope.message = "";
    $http({
      method: 'GET',
      url: "/verify"
    }).success(function(data) {
         $scope.message = data.msg; // JSON response 
   });

  }]);

However, I end up with an empty key variable when I try to print it out. I recently used nginx to take out my .html extension if that's a possible cause of this problem. How do I fix this?

答案1

得分: 0

解决我的问题涉及检查请求的URL链接:

log.Print(r.URL) // 这将返回"/verify"

然而,这并不完全是你想要的。相反,你想要完整的URL。你可以通过以下方式获取完整的URL并从中提取参数:

urlStr := r.Referer()             // 将完整的URL作为字符串获取
urlFull, err := url.Parse(urlStr) // 返回一个*URL对象
if err != nil {
    log.Fatal(err)
    return
}
key := urlFull.Query().Get("key") // 现在我们从URL中获取key参数
log.Println("Key: " + key) // 现在你将得到一个非空字符串
英文:

The solution to my question involves inspecting the request URL link by

log.Print(r.URL) // This returns "/verify"

However, that's not exactly what you want. Instead, you want the full URL. You can do the following to get the full URL and extract the parameter from it:

urlStr := r.Referer()             // gets the full URL as a string
urlFull, err := url.Parse(urlStr) // returns a *URL object
if err != nil {
	log.Fatal(err)
	return
}
key := urlFull.Query().Get("key") // now we get the key parameter from the URL
log.Println("Key: " + key) // now you'll get a non empty string

huangapple
  • 本文由 发表于 2015年8月1日 03:09:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/31753467.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定