-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTester_API.go
108 lines (100 loc) · 2.76 KB
/
Tester_API.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"errors"
"io/ioutil"
)
type Client struct{
ID uint32
Name string
Phone string
Adress string
Ordre string
State uint32
}
func AddAnOrdre() error {
url := "http://localhost:8080/Commande"
data := Client{Name: "hamidhamid", Phone: "0632343698", Adress: "Benguerir", Ordre: "Kabba chamiya", State: 0}
payload, err := json.Marshal(data)
if err != nil {
return errors.New("Json Encoding")
}
fmt.Println(payload)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return errors.New("Setuping The Request")
}
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.New("Sending The request")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.New("Reading The response body")
}
fmt.Println(resp.Status)
fmt.Println(string(body))
return nil
}
func Get_the_DB() error {
url := "http://localhost:8080/Admin"
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return errors.New("Setuping The Request")
}
req.Header.Set("Authorization", "bearer mLGV?uxpQ0rfsy-))CGW.1!=r#dKOiZe5#G]|B4Xi@tPuVF2SdubREaTAK#}DO3I")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.New("Sending The request")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.New("Reading The response body")
}
fmt.Println(resp.Status)
fmt.Println(string(body))
return nil
}
func ChangeState() error {
url := "http://localhost:8080/Admin"
data := 4
payload, err := json.Marshal(data)
if err != nil {
return errors.New("Json Encoding")
}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return errors.New("Setuping The Request")
}
req.Header.Set("Authorization", "bearer mLGV?uxpQ0rfsy-))CGW.1!=r#dKOiZe5#G]|B4Xi@tPuVF2SdubREaTAK#}DO3I")
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.New("Sending The request")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return errors.New("Reading The response body")
}
fmt.Println(resp.Status)
fmt.Println(string(body))
return nil
}
var err error
func main() {
// err = Get_the_DB()
// err = ChangeState()
// err = Get_the_DB()
err = AddAnOrdre()
if (err != nil){
fmt.Println("Error: ", err)
}
}
// {"Name": "Hamid", "Phone": "0623313463", "Adress": "Rabat", "Commande": "Tacos"}