Go doing a GET request and building the Querystring

后端 未结 3 561
伪装坚强ぢ
伪装坚强ぢ 2020-12-07 11:53

I am pretty new to Go and don\'t quite understand everything as yet. In many of the modern languages Node.js, Angular, jQuery, PHP you can do a GET request with additional q

相关标签:
3条回答
  • 2020-12-07 12:36

    As a commenter mentioned you can get Values from net/url which has an Encode method. You could do something like this (req.URL.Query() returns the existing url.Values)

    package main
    
    import (
        "fmt"
        "log"
        "net/http"
        "os"
    )
    
    func main() {
        req, err := http.NewRequest("GET", "http://api.themoviedb.org/3/tv/popular", nil)
        if err != nil {
            log.Print(err)
            os.Exit(1)
        }
    
        q := req.URL.Query()
        q.Add("api_key", "key_from_environment_or_flag")
        q.Add("another_thing", "foo & bar")
        req.URL.RawQuery = q.Encode()
    
        fmt.Println(req.URL.String())
        // Output:
        // http://api.themoviedb.org/3/tv/popular?another_thing=foo+%26+bar&api_key=key_from_environment_or_flag
    }
    

    http://play.golang.org/p/L5XCrw9VIG

    0 讨论(0)
  • 2020-12-07 12:39

    Use r.URL.Query() when you appending to existing query, if you are building new set of params use the url.Values struct like so

    package main
    
    import (
        "fmt"
        "log"
        "net/http"
        "net/url"
        "os"
    )
    
    func main() {
        req, err := http.NewRequest("GET","http://api.themoviedb.org/3/tv/popular", nil)
        if err != nil {
            log.Print(err)
            os.Exit(1)
        }
    
        // if you appending to existing query this works fine 
        q := req.URL.Query()
        q.Add("api_key", "key_from_environment_or_flag")
        q.Add("another_thing", "foo & bar")
    
        // or you can create new url.Values struct and encode that like so
        q := url.Values{}
        q.Add("api_key", "key_from_environment_or_flag")
        q.Add("another_thing", "foo & bar")
    
        req.URL.RawQuery = q.Encode()
    
        fmt.Println(req.URL.String())
        // Output:
        // http://api.themoviedb.org/3/tv/popularanother_thing=foo+%26+bar&api_key=key_from_environment_or_flag
    }
    
    0 讨论(0)
  • 2020-12-07 12:41

    Using NewRequest just to create an URL is an overkill. Use the net/url package:

    package main
    
    import (
        "fmt"
        "net/url"
    )
    
    func main() {
        base, err := url.Parse("http://www.example.com")
        if err != nil {
            return
        }
    
        // Path params
        base.Path += "this will get automatically encoded"
    
        // Query params
        params := url.Values{}
        params.Add("q", "this will get encoded as well")
        base.RawQuery = params.Encode() 
    
        fmt.Printf("Encoded URL is %q\n", base.String())
    }
    

    Playground: https://play.golang.org/p/YCTvdluws-r

    0 讨论(0)
提交回复
热议问题