master
Evan, Chen 2021-08-06 15:00:21 +08:00
commit 72cbc1ce73
3 changed files with 69 additions and 0 deletions

2
Makefile Normal file
View File

@ -0,0 +1,2 @@
run:
go run main.go

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module request
go 1.16

64
main.go Normal file
View File

@ -0,0 +1,64 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"time"
)
var host = "http://demo.arec.com/api/"
func main() {
tok := GetToken()
req, _ := http.NewRequest("GET", host+"recorder/status", nil)
req.Header.Set("Authorization", tok)
client := &http.Client{
Timeout: time.Second * 10,
}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer res.Body.Close()
fmt.Println(res.Status)
body, _ := ioutil.ReadAll(res.Body)
resData := map[string]interface{}{}
json.Unmarshal(body, &resData)
fmt.Println(resData)
}
func GetToken() string {
// a := "sdf"
// []byte(a)
values := map[string]string{"authorization": "YWRtaW46YWRtaW4="}
data, _ := json.Marshal(values)
req, _ := http.NewRequest("POST", host+"token", bytes.NewReader(data))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{
Timeout: time.Second * 10,
}
res, err := client.Do(req)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer res.Body.Close()
fmt.Println(res.Status)
body, _ := ioutil.ReadAll(res.Body)
resData := map[string]interface{}{}
json.Unmarshal(body, &resData)
fmt.Println(resData["token"])
return fmt.Sprintf("%v", resData["token"])
}