Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lesson6/client_post.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
defer httpResp.Body.Close()
log.Printf("Status: %s", httpResp.Status)
io.Copy(os.Stdout, httpResp.Body)
}
Expand Down
3 changes: 3 additions & 0 deletions lesson6/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ func main() {
http.Handle("/", httputil.NewSingleHostReverseProxy(&target))
log.Printf("Proxy started")
http.ListenAndServe("localhost:8081", nil)
if err := http.ListenAndServe("localhost:8081", nil); err != nil {
log.Fatal(err)
}
}
5 changes: 5 additions & 0 deletions lesson6/web_server_body.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
)

func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
fmt.Println("The body is:")
io.Copy(os.Stdout, req.Body)
})
http.ListenAndServe("localhost:8080", nil)
if err := http.ListenAndServe("localhost:8080", nil); err != nil {
log.Fatal(err)
}
}
6 changes: 6 additions & 0 deletions lesson6/web_server_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)

func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
defer req.Body.Close()
var params map[string]interface{}
err := json.NewDecoder(req.Body).Decode(&params)
if err != nil {
fmt.Printf("Error parsing body: %s", err)
http.Error(w, fmt.Sprintf("Error parsing body: %s", err), http.StatusBadRequest)
return
}
fmt.Printf("The body is: %#v", params)
})
http.ListenAndServe("localhost:8080", nil)
if err := http.ListenAndServe("localhost:8080", nil); err != nil {
log.Fatal(err)
}
}
5 changes: 5 additions & 0 deletions lesson6/web_server_json_2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)

Expand All @@ -14,6 +15,7 @@ type Params struct {
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, req *http.Request) {
var params Params
defer req.Body.Close()
err := json.NewDecoder(req.Body).Decode(&params)
if err != nil {
http.Error(w, fmt.Sprintf("Error parsing body: %s", err), http.StatusBadRequest)
Expand All @@ -22,4 +24,7 @@ func main() {
fmt.Fprintf(w, "Hello %s", params.Hello)
})
http.ListenAndServe("localhost:8080", nil)
if err := http.ListenAndServe("localhost:8080", nil); err != nil {
log.Fatal(err)
}
}