Home > OS >  How to make fmt.Sprint spring work for parameters in a URL?
How to make fmt.Sprint spring work for parameters in a URL?

Time:02-20

I have a reverse-proxy that returns body responses from a 3rd party API. This 3rd party API uses pagination, so my reverse-proxy path requires parameters for page number.

I'm having trouble using fmt.Sprint to pass the parameter from the reverse-proxy URL to 3rd Party API request.

func (s *Server) getReverseProxy(w http.ResponseWriter, r *http.Request) {
    keys, ok := r.URL.Query()["page"]

    if !ok || len(keys[0]) < 1 {
        log.Println("Url Param 'page' is missing")
        return
    }

    // Query()["key"] will return an array of items,
    // we only want the single item.
    key := keys[0]

    log.Println("Url Param 'page' is: "   string(key))

    // create http client to make GET request to reverse-proxy
    client := &http.Client{}

    // create 3rd party request

    // creating this request is causing me the issue due to the page parameter 
    req, err := http.NewRequest("GET", fmt.Sprint("https://url.com/path?&page[size]=100&page[number]=%s\n", key), nil)

    // more stuff down here but omitted for brevity.
}

Looking at the http.NewRequest 3rd party api request, the %s\n part would be where they key is passed for the page parameter.

How can I correctly pass this variable to the url? In python what I'm looking to use is an f-string. Not sure im doing that correctly for Go.

EDIT: New implementation using url.Vaues()

func (s *Server) getReverseProxy(w http.ResponseWriter, r *http.Request) {
    keys, ok := r.URL.Query()["page"]

    if !ok || len(keys[0]) < 1 {
        log.Println("Url Param 'page' is missing")
        return
    }

    // Query()["key"] will return an array of items,
    // we only want the single item.
    key := keys[0]

    log.Println("Url Param 'page' is: "   string(key))
    params := url.Values{
        "page[size]":        []string{"100"},
        "page["   key   "]": []string{"1"},
    }
    u := &url.URL{
        Scheme:   "https",
        Host:     "url.com",
        Path:     "/path",
        RawQuery: params.Encode(),
    }

    // create http client to make GET request
    client := &http.Client{}

    // create a variable for the JSON model expected from Terraform Cloud API
    var data models.TerraformResponse
    req, err := http.NewRequest("GET", u.String(), nil)
}

CodePudding user response:

You should probably be constructing the URL and query using the net/url package. This has the advantage that it is much safer.

params := url.Values{
    "page[size]":        []string{"100"},
    "page["   key   "]": []string{"1"},
}
u := &url.URL{
    Scheme:   "https",
    Host:     "url.com",
    Path:     "/path",
    RawQuery: params.Encode(),
}
req, err := http.NewRequest("GET", u.String(), nil)

Trying to use fmt.Sprintf() to construct URLs is a bit more likely to backfire.

If you want to construct a URL with fmt.Sprintf, you’ll need to escape all the % in the format string, and escape special characters in the arguments.

fmt.Sprint("https://url.com/path?&page%[size%]=100&page%[%s%]=1",
    url.QueryEscape(key))

The url.QueryEscape() function escapes the characters in a string so it can be safely placed in a URL query. It’s not necessary if you construct the URL with url.Values and url.URL.

  •  Tags:  
  • go
  • Related