package util
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
)
/**
发送的POST请求 需要设置header
Testner 20210123
*/
func Post_Json(url string, bodyJson string,authorization string) string {
contentType := "application/json"
req,err := http.NewRequest("POST",url,bytes.NewBuffer([]byte(bodyJson)))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authorization))
req.Header.Set("Content-Type", contentType)
resp ,err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
byts,err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
log.Fatal(err)
}
return (string(byts))
}
/**
发送的GET请求 需要设置header
Testner 20210123
*/
func Get_Json(url string, authorization string) string {
contentType := "application/json"
req,err := http.NewRequest("GET",url, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", authorization))
req.Header.Set("Content-Type", contentType)
resp ,err := http.DefaultClient.Do(req)
if err != nil {
log.Fatal(err)
}
byts,err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
log.Fatal(err)
}
return (string(byts))
}