post explained

This commit is contained in:
Mert Gör ☭ 2024-04-13 13:37:26 +03:00
parent 3fe315e0f9
commit 32cffbd2bf
Signed by: hwpplayer1
GPG Key ID: 03E547D043AB6C8F
2 changed files with 26 additions and 0 deletions

View File

@ -130,4 +130,5 @@ func main() {
string_functions.Demo1()
string_functions.Demo2()
restful.Demo1()
restful.Demo2()
}

View File

@ -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)
}