Created
November 14, 2019 16:34
-
-
Save liangchaoboy/6a6043684ee7fdf38ffc1f44ccdc3779 to your computer and use it in GitHub Desktop.
qiniu
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"github.com/qiniu/api.v7/auth" | |
"github.com/qiniu/api.v7/auth/qbox" | |
"io/ioutil" | |
"net/http" | |
"bytes" | |
) | |
var ( | |
FusionHost = "http://fusion.qiniuapi.com" | |
) | |
type RefreshStr struct { | |
RequestId string`json:"request_id"` | |
IsDir string `json:"is_dir"` | |
Urls []string `json:"urls"` | |
State string `json:"state"` | |
PageNo int `json:"page_no"` | |
PageSize int `json:"page_size"` | |
} | |
func main() { | |
mac := qbox.NewMac("ak", "sk") | |
t := RefreshStr{ | |
RequestId:"5dcd7d4317b0674100125214", | |
//IsDir:"no", | |
//Urls:["http://limit-api.support2technical.me/2.png"], | |
} | |
p, _ := postRequest(mac, "/v2/tune/refresh/list",t) | |
fmt.Println(string(p)) | |
} | |
// RequestWithBody 带body对api发出请求并且返回response body | |
func postRequest(mac *auth.Credentials, path string, body interface{}) (resData []byte, | |
err error) { | |
urlStr := fmt.Sprintf("%s%s", FusionHost, path) | |
fmt.Println(urlStr) | |
reqData, _ := json.Marshal(body) | |
req, reqErr := http.NewRequest("POST", urlStr, bytes.NewReader(reqData)) | |
if reqErr != nil { | |
err = reqErr | |
return | |
} | |
accessToken, signErr := mac.SignRequest(req) | |
fmt.Println(req.URL,req.Body) | |
if signErr != nil { | |
err = signErr | |
return | |
} | |
req.Header.Add("Authorization", "QBox "+accessToken) | |
req.Header.Add("Content-Type", "application/json") | |
resp, respErr := http.DefaultClient.Do(req) | |
if respErr != nil { | |
err = respErr | |
return | |
} | |
defer resp.Body.Close() | |
resData, ioErr := ioutil.ReadAll(resp.Body) | |
if ioErr != nil { | |
err = ioErr | |
return | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment