Skip to content

Instantly share code, notes, and snippets.

@narenaryan
Last active May 29, 2024 02:42
Show Gist options
  • Save narenaryan/e84dc60f4c09657041827f491736c089 to your computer and use it in GitHub Desktop.
Save narenaryan/e84dc60f4c09657041827f491736c089 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"time"
)
func main() {
// Create a new transport
transport := &http.Transport{
MaxIdleConns: 1,
MaxIdleConnsPerHost: 1,
IdleConnTimeout: 30 * time.Second,
}
// Create a new client with transport
client := &http.Client{Transport: transport, Timeout: 5 * time.Second}
defer client.CloseIdleConnections()
// Create a new HTTP request
req, err := http.NewRequest("GET", "https://httpbin.org/get", nil)
if err != nil {
fmt.Println(err)
return
}
// Attach headers to the request
req.Header.Add("User-Agent", "go-http-client")
req.Header.Add("Content-Type", "application/json")
// Send the request using client
resp, err := client.Do(req)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(resp.StatusCode)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment