go-request-example/main.go

65 lines
1.2 KiB
Go

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"])
}