Sling Academy
Home/Golang/How to send GET requests with params in Go

How to send GET requests with params in Go

Last updated: November 27, 2024

Sending HTTP GET requests with parameters in Go is straightforward thanks to the net/http package, which is a part of the Go standard library. In this article, we will explore how to build a simple HTTP GET request and add query parameters to it.

Step 1: Setting Up Your Go Project

First, create a new directory for your Go project and initialize a Go module:

mkdir my-go-get-request
cd my-go-get-request
go mod init my-go-get-request

Step 2: Importing Required Packages

In your main Go file, you need to import the necessary packages:

package main

import (
    "fmt"
    "net/http"
    "net/url"
    "io/ioutil"
    "log"
)

Step 3: Create a HTTP Client

A HTTP client is required to make requests:

func main() {
    client := &http.Client{}

    // Construct request
    baseUrl := "http://example.com/api"
    // Setup query parameters
    endpoint, err := url.Parse(baseUrl)
    if err != nil {
        log.Fatal(err)
    }

    queryParams := url.Values{}
    queryParams.Set("param1", "value1")
    queryParams.Set("param2", "value2")

    endpoint.RawQuery = queryParams.Encode()

Step 4: Sending GET Request

Now that you have set up your request and parameters, you can send the GET request and handle the response:

    // Create request with merged query params
    req, err := http.NewRequest("GET", endpoint.String(), nil)
    if err != nil {
        log.Fatal(err)
    }

    // Execute request
    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }
    defer resp.Body.Close()

Step 5: Reading and Handling the Response

Reading the contents of the server's response can inform us whether our request was successful:

    // Read response body
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Println(string(body))
}

Conclusion

You now have a basic Go application that sends HTTP GET requests with parameters. This is an important skill for accessing RESTful APIs and integrating services in Go. Make sure to properly handle any errors and incorporate logging as needed for production-ready applications.

Next Article: How to send POST requests with body and headers in Go

Previous Article: Networking Best Practices for Go Applications

Series: Networking and Server

Golang

Related Articles

You May Also Like

  • How to remove HTML tags in a string in Go
  • How to remove special characters in a string in Go
  • How to remove consecutive whitespace in a string in Go
  • How to count words and characters in a string in Go
  • Relative imports in Go: Tutorial & Examples
  • How to run Python code with Go
  • How to generate slug from title in Go
  • How to create an XML sitemap in Go
  • How to redirect in Go (301, 302, etc)
  • Using Go with MongoDB: CRUD example
  • Auto deploy Go apps with CI/ CD and GitHub Actions
  • Fixing Go error: method redeclared with different receiver type
  • Fixing Go error: copy argument must have slice type
  • Fixing Go error: attempted to use nil slice
  • Fixing Go error: assignment to constant variable
  • Fixing Go error: cannot compare X (type Y) with Z (type W)
  • Fixing Go error: method has pointer receiver, not called with pointer
  • Fixing Go error: assignment mismatch: X variables but Y values
  • Fixing Go error: array index must be non-negative integer constant