Last active
May 29, 2024 02:42
-
-
Save narenaryan/e84dc60f4c09657041827f491736c089 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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