How to handle HTTP Get response?
Fprintln allows you to direct output to any writer. Feel free to choose the available port of your choice - higher ports will make it easier to bypass the built-in security functionality in any system. net/http directs requests using a URI or URL endpoint to helper functions, which must implement the http.ResponseWriter and http.Request methods.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8" /> </head> <body> <div> <form method="POST" action="/"> <label>Name</label><input name="name" type="text" value=""> <label>Name</label><input name="" type="text" value=""> <input type="submit" value="submit" /> </form> </div> </body> </html>
Example
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func ServeHTTP(w http.ResponseWriter, r *http.Request) {
url := "http://country.io/capital.json"
response, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
responseData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
responseString := string(responseData)
fmt.Fprint(w, responseString)
}
func main() {
http.HandleFunc("/", ServeHTTP)
if err := http.ListenAndServe(":8080", nil); err != nil {
log.Fatal(err)
}
}
Most Helpful This Week
Example of Sscan vs Sscanf vs Sscanln from FMT Package
Example of Fscan, Fscanf, and Fscanln from FMT Package
How to check if a string contains a substring in Golang?
Regular expression to extract all Non-Alphanumeric Characters from a String
How to find length of Map in Go?
Example: How to use ReadAtLeast from IO Package in Golang?
How to fetch an Integer variable as String in Go?
How to add Watermark or Merge two image?
How to check if a string contains a numbers in Golang?
Different ways to convert Byte Array into String