tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/net/http-head/main.go (about) 1 // This example gets an URL using http.Head(). URL scheme can be http or https. 2 // 3 // Note: It may be necessary to increase the stack size when using "net/http". 4 // Use the -stack-size=4KB command line option. 5 // 6 // Some targets (Arduino Nano33 IoT) don't have enough SRAM to run http.Head(). 7 // Use the following for those targets: 8 // 9 // examples/net/webclient (for HTTP) 10 // examples/net/tlsclient (for HTTPS) 11 12 //go:build ninafw || wioterminal 13 14 package main 15 16 import ( 17 "bytes" 18 "fmt" 19 "log" 20 "machine" 21 "net/http" 22 "time" 23 24 "tinygo.org/x/drivers/netlink" 25 "tinygo.org/x/drivers/netlink/probe" 26 ) 27 28 var ( 29 ssid string 30 pass string 31 url string = "https://httpbin.org" 32 ) 33 34 func main() { 35 36 waitSerial() 37 38 link, _ := probe.Probe() 39 40 err := link.NetConnect(&netlink.ConnectParams{ 41 Ssid: ssid, 42 Passphrase: pass, 43 }) 44 if err != nil { 45 log.Fatal(err) 46 } 47 48 resp, err := http.Head(url) 49 if err != nil { 50 log.Fatal(err) 51 } 52 defer resp.Body.Close() 53 54 var buf bytes.Buffer 55 if err := resp.Write(&buf); err != nil { 56 log.Fatal(err) 57 } 58 fmt.Println(string(buf.Bytes())) 59 60 link.NetDisconnect() 61 } 62 63 // Wait for user to open serial console 64 func waitSerial() { 65 for !machine.Serial.DTR() { 66 time.Sleep(100 * time.Millisecond) 67 } 68 }