tinygo.org/x/drivers@v0.27.1-0.20240509133757-7dbca2a54349/examples/net/http-postform/main.go (about) 1 // This example posts an URL using http.PostForm(). 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 7 // http.PostForm(). 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 "fmt" 18 "io" 19 "log" 20 "machine" 21 "net/http" 22 "net/url" 23 "time" 24 25 "tinygo.org/x/drivers/netlink" 26 "tinygo.org/x/drivers/netlink/probe" 27 ) 28 29 var ( 30 ssid string 31 pass string 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 path := "https://httpbin.org/post" 49 data := url.Values{ 50 "name": {"John Doe"}, 51 "occupation": {"gardener"}, 52 } 53 54 resp, err := http.PostForm(path, data) 55 if err != nil { 56 log.Fatal(err) 57 } 58 defer resp.Body.Close() 59 60 body, err := io.ReadAll(resp.Body) 61 if err != nil { 62 log.Fatal(err) 63 } 64 65 fmt.Println(string(body)) 66 } 67 68 // Wait for user to open serial console 69 func waitSerial() { 70 for !machine.Serial.DTR() { 71 time.Sleep(100 * time.Millisecond) 72 } 73 }