TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang http.Get Examples: Download Web Pages

Use the net http package to download web pages. Specify http.Get, Body and Status.
Http.Get. Golang can be used to download web pages. This helps with validating a site works correctly (or for trying to get important data from the web page).
With the net http package, we have many helpful funcs for accessing files through HTTP. We do not need to write any low-level code to do this.
Http.Get example. Let us begin with this example program that uses http.Get. Please note we must import the "net/http" package at the top.

Get: We use the Get() func to download the home page of Wikipedia—the HTTPS protocol is used here.

Defer: We require that the Body (a stream) is entirely closed before continuing. The defer keyword waits for the Body to close.

ReadAll: We use the ioutil package to access the ReadAll method—this func is useful in many Go programs.

File
Golang program that uses http.Get, Body package main import ( "fmt" "io/ioutil" "net/http" ) func main() { // Get Wikipedia home page. resp, _ := http.Get("https://en.wikipedia.org/") // Defer close the response Body. defer resp.Body.Close() // Read everything from Body. body, _ := ioutil.ReadAll(resp.Body) // Convert body bytes to string. bodyText := string(body) fmt.Println("DONE") fmt.Println("Response length: ", len(bodyText)) // Convert to rune slice to take substring. runeSlice := []rune(bodyText) fmt.Println("First chars: ", string(runeSlice[0:10])) } Output DONE Response length: 73727 First chars: <!DOCTYPE
Status. When a HTTP request is made, the response includes a status code. We can access the string containing the status with "Status."

And: We can access the int code itself with StatusCode. This is probably easier to use when testing for a 404 error.

Tip: When using http.Get(), a 404 error is not returned as an error in Go. It is returned as a successful download with a 404 code.

Golang program that uses Status, StatusCode package main import ( "fmt" "net/http" "strings" ) func main() { // This file does not exist. resp, _ := http.Get("https://www.dotnetCodex.com/robots.txt") defer resp.Body.Close() // Test status string. fmt.Println("Status:", resp.Status) if strings.Contains(resp.Status, "404") { fmt.Println("Is not found (string tested)") } // Test status code of the response. fmt.Println("Status code:", resp.StatusCode) if resp.StatusCode == 404 { fmt.Println("Is not found (int tested)") } } Output Status: 404 Not Found Is not found (string tested) Status code: 404 Is not found (int tested)
Serve. With the ListenAndServe func, also part of the http module, we can build a simple web server in Golang. Each response can have a special function with HandleFunc.ListenAndServe
A review. Making sure your website is working correctly is an important job. And Golang is well-suited to download pages and validate them.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf