diff --git a/main.go b/main.go index 9789ca2..463d159 100644 --- a/main.go +++ b/main.go @@ -130,4 +130,5 @@ func main() { string_functions.Demo1() string_functions.Demo2() restful.Demo1() + restful.Demo2() } diff --git a/restful/demo1.go b/restful/demo1.go index c2b0402..b569885 100644 --- a/restful/demo1.go +++ b/restful/demo1.go @@ -1,6 +1,7 @@ package restful import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -30,4 +31,28 @@ func Demo1() { var todo Todo json.Unmarshal(bodyBytes, &todo) fmt.Println(todo) +} + +func Demo2() { + todo := Todo{1,2, "Go to market", false} + jsonTodo,err := json.Marshal(todo) + + response, err := http.Post("https://jsonplaceholder.typicode.com/todos", "application/json;charset=utf-8", bytes.NewBuffer(jsonTodo)) + + if err != nil{ + fmt.Println(err) + } + + defer response.Body.Close() + + bodyBytes,_ := ioutil.ReadAll(response.Body) + + bodyString := string(bodyBytes) + fmt.Println(bodyString) + + var todoResponse Todo + json.Unmarshal(bodyBytes, &todoResponse) + fmt.Println(todo) + + } \ No newline at end of file