Add/Set URL Parameters on the http.Request struct

26,400 views
Skip to first unread message

mark...@gmail.com

unread,
May 29, 2014, 7:51:28 AM5/29/14
to golan...@googlegroups.com
Hi,

I am trying to update query parameters within a "http.Request". I expected to be able to do the following, however, the values are never updated. 

ret := httpRequest.URL.Query().Get(name)

if len(ret) == 0 {
    httpRequest.URL.Query().Add(name, value)
} else {
    httpRequest.URL.Query().Set(name, value)
}

The only way I could get it to work is in the QuerySet function, but it feels a bit overkill?

func QuerySet(httpRequest *http.Request, name string, value string) (err error) {
ret := httpRequest.URL.Query().Get(name)

if len(ret) == 0 {
params := url.Values{}
for k, v := range httpRequest.URL.Query() {
params.Add(k, v[0] )
}

params.Add(name, value)
httpRequest.URL.RawQuery = params.Encode()

} else {
params := url.Values{}
for k, v := range httpRequest.URL.Query() {
params.Add(k, v[0] )
}

params.Set(name, value)
httpRequest.URL.RawQuery = params.Encode()
}

        return nil
}

Thanks
Mark

Matt Harden

unread,
May 29, 2014, 9:34:16 PM5/29/14
to mark...@gmail.com, golang-nuts
values := httpRequest.URL.Query()
values.Add(name, value)
httpRequest.URL.RawQuery = values.Encode()



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

te...@rapidtrade.biz

unread,
Dec 14, 2018, 1:00:42 PM12/14/18
to golang-nuts
Thank you very much

Prateek Singhal

unread,
Oct 30, 2022, 1:27:16 PM10/30/22
to golang-nuts
This was to add query parameters but can anyone help me on how to add path parameters
like 
where sub-path is the path parameter having a string value like "about"
so the link becomes 
"http://mywebsite.com/about"
On Friday, December 14, 2018 at 11:30:42 PM UTC+5:30 te...@rapidtrade.biz wrote:
Thank you very much
Reply all
Reply to author
Forward
0 new messages