Golang HTTP GET request with parameters
Example-1
To make an HTTP GET request with parameters in Go, you can use the http package and the net/url package to build the URL with the parameters. Here is an example of how to do this:
Example
package main
import (
"fmt"
"net/http"
"net/url"
)
func main() {
baseURL := "http://example.com"
resource := "/path"
params := url.Values{}
params.Add("param1", "value1")
params.Add("param2", "value2")
u, _ := url.ParseRequestURI(baseURL)
u.Path = resource
u.RawQuery = params.Encode()
urlStr := fmt.Sprintf("%v", u) // "http://example.com/path?param1=value1¶m2=value2"
resp, err := http.Get(urlStr)
// handle error and response
}
Example-2
Here is an example of making an HTTP GET request in Go with parameters in the query string:
Example
package main
import (
"fmt"
"net/http"
"net/url"
)
func main() {
baseURL, _ := url.Parse("http://example.com")
params := url.Values{}
params.Add("param1", "value1")
params.Add("param2", "value2")
baseURL.RawQuery = params.Encode()
resp, _ := http.Get(baseURL.String())
defer resp.Body.Close()
fmt.Println(resp.Status)
}
Example-3
To make an HTTP GET request with parameters in Go, you can use the net/http package. Here's an example of how to do it:
Example
package main
import (
"fmt"
"net/http"
"net/url"
)
func main() {
// Define the parameters
params := url.Values{}
params.Add("param1", "value1")
params.Add("param2", "value2")
// Create the URL with the parameters
url := "https://example.com?" + params.Encode()
// Make the GET request
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
// Do something with the response
// ...
}
Most Helpful This Week
Illustration of Sleeping Barber Problem in Golang
Find out how many logical processors used by current process
State and Props in React
Golang program for implementation of Linear Search
GO Program to Calculate Sum of Natural Numbers Using for.....Loop
How do you set cookies in an HTTP request with an HTTP client in Go?
How do you create an HTTP client in Go?
What is risk management and why is it important?
Invalid memory address or nil pointer dereference error in Golang
3 Different Examples - How State Works in React?
Most Helpful This Week
How to use function from another file golang?How to split a string on white-space?Subtract N number of Year, Month, Day, Hour, Minute, Second, Millisecond, Microsecond and Nanosecond to current date-time.Example: Arrays of Arrays, Arrays of Slices, Slices of Arrays and Slices of SlicesHow to append text to a file in Golang?Get Set and Clear Session in GolangCopy an array by value and reference into another arrayHow To Make an HTTP Server in Golang?Example of Sscan vs Sscanf vs Sscanln from FMT PackageHow to convert Go struct to JSON?