Skip to content
Snippets Groups Projects
Commit 9a9d8818 authored by Marcus Schiesser's avatar Marcus Schiesser
Browse files

implement POST request for gowww client

parent 19c1cc9c
No related branches found
No related tags found
No related merge requests found
package main package main
import ( import (
"io" "bytes"
"encoding/json"
"log" "log"
"net/http" "net/http"
) )
...@@ -10,16 +11,31 @@ const ( ...@@ -10,16 +11,31 @@ const (
url = "http://localhost:8080/hellowww" url = "http://localhost:8080/hellowww"
) )
type helloWWWRequest struct {
Name string `json:"name"`
}
type helloWWWResponse struct {
Message string `json:"message"`
}
func main() { func main() {
log.Printf("Calling service %s\n", url) log.Printf("Client is sending a request to %v\n", url)
resp, err := http.Get(url) request := helloWWWRequest{Name: "Marcus"}
data, err := json.Marshal(&request)
if err != nil { if err != nil {
log.Fatalf("Error reading from %s\n%#v", url, err) log.Fatalf("Error while marshalling request: %v", err)
}
resp, err := http.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
log.Fatalf("Error while sending request %s: %v", url, err)
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := io.ReadAll(resp.Body) decoder := json.NewDecoder(resp.Body)
var response helloWWWResponse
err = decoder.Decode(&response)
if err != nil { if err != nil {
log.Fatalf("Error reading from %s\n%#v", url, err) log.Fatalf("Error reading from %s\n%#v", url, err)
} }
log.Printf("Result: %s", body) log.Printf("Message: %s", response.Message)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment