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.Demo1()
|
||||||
string_functions.Demo2()
|
string_functions.Demo2()
|
||||||
restful.Demo1()
|
restful.Demo1()
|
||||||
|
restful.Demo2()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package restful
|
package restful
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -31,3 +32,27 @@ func Demo1() {
|
||||||
json.Unmarshal(bodyBytes, &todo)
|
json.Unmarshal(bodyBytes, &todo)
|
||||||
fmt.Println(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