kernelpanek

Simple Webpage Fetcher

Feb 18th, 2023
1,321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.67 KB | Software | 0 0
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5.     "io"
  6.     "net/http"
  7. )
  8.  
  9. func main() {
  10.     // Create an HTTP client with default settings
  11.     client := &http.Client{}
  12.  
  13.     // Make an HTTPS GET request to example.com
  14.     resp, err := client.Get("https://pastebin.com")
  15.     if err != nil {
  16.         fmt.Println("Error:", err)
  17.         return
  18.     }
  19.     defer resp.Body.Close()
  20.  
  21.     // Print the response body
  22.     fmt.Println(resp.Status)
  23.     fmt.Println(resp.Header)
  24.     // Read the response body and convert it to a string
  25.  
  26.     bodyBytes, err := io.ReadAll(resp.Body)
  27.     if err != nil {
  28.         fmt.Println("Error:", err)
  29.         return
  30.     }
  31.     bodyString := string(bodyBytes)
  32.  
  33.     // Print the response body as a string
  34.     fmt.Println(bodyString)
  35. }
  36.  
Tags: http webclient
Advertisement