post explained
This commit is contained in:
parent
3fe315e0f9
commit
32cffbd2bf
2 changed files with 26 additions and 0 deletions
1
main.go
1
main.go
|
@ -130,4 +130,5 @@ func main() {
|
|||
string_functions.Demo1()
|
||||
string_functions.Demo2()
|
||||
restful.Demo1()
|
||||
restful.Demo2()
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
||||
}
|
Loading…
Add table
Reference in a new issue