-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added golang http client and server on net/http lib
- Loading branch information
Showing
3 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package client | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"bufio" | ||
) | ||
|
||
func HTTPClientGet() { | ||
resp, err := http.Get("http://localhost:8080") | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
fmt.Println("Response status:", resp.Status) | ||
|
||
scanner := bufio.NewScanner(resp.Body) | ||
for i := 0; scanner.Scan() && i < 5; i++ { | ||
fmt.Println(scanner.Text()) | ||
} | ||
|
||
if err := scanner.Err(); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func HTTPClientHeaderGet() { | ||
client := &http.Client{} | ||
req, _ := http.NewRequest("GET", "http://localhost:8080/headers", nil) | ||
req.Header.Add("test", "test") | ||
resp, err := client.Do(req) | ||
|
||
if err != nil { | ||
panic(err) | ||
} | ||
defer resp.Body.Close() | ||
for name, headers := range resp.Header { | ||
for _, h := range headers { | ||
fmt.Printf("Header: %v: %v\n", name, h) | ||
} | ||
} | ||
fmt.Println("Response status:", resp.Status) | ||
|
||
scanner := bufio.NewScanner(resp.Body) | ||
for i := 0; scanner.Scan() && i < 5; i++ { | ||
fmt.Println(scanner.Text()) | ||
} | ||
if err := scanner.Err(); err != nil { | ||
panic(err) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package server | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func some(writer http.ResponseWriter, req *http.Request) { | ||
fmt.Fprintf(writer, "Something in the water!\n") | ||
} | ||
|
||
func headers(writer http.ResponseWriter, req *http.Request) { | ||
for name, headers := range req.Header { | ||
for _, h := range headers { | ||
fmt.Fprintf(writer, "%v: %v\n", name, h) | ||
} | ||
} | ||
} | ||
|
||
func HTTPServer() { | ||
http.HandleFunc("/some", some) | ||
http.HandleFunc("/headers", headers) | ||
|
||
err := http.ListenAndServe(":8080", nil) | ||
if err != nil { | ||
fmt.Println(err) | ||
} | ||
} |