ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] > [github](https://github.com/parnurzeal/gorequest) ## 安装 `go get github.com/parnurzeal/gorequest` ## 使用 ### 简单 GET/POST/PUT ... ``` resp, body, errs := gorequest.New().Get("http://example.com/").End() // PUT -> request.Put("http://example.com").End() // DELETE -> request.Delete("http://example.com").End() // HEAD -> request.Head("http://example.com").End() // ANYTHING -> request.CustomMethod("TRACE", "http://example.com").End() ``` ### get 带参数 ``` gorequest.New().Post("http://127.0.0.1:8010/test.php").Query(send{ Name: "ads", Age: 123, }).End() ``` ### post 表单提交 //主要通过控制type进行配合 ``` //以表单形式提交,只能用multipart _, s, errors := gorequest.New().Post("http://127.0.0.1:8010/test.php").Type("multipart").Send(send{ Name: "asd", Age: 123, }).End() ``` 可多个 struct 一起发送 ``` _, s, errors := agent.Post("http://127.0.0.1:8010/test.php").Type("multipart").Send(send2{ Age2: 123, }).Send(send{ Name: "asd", Age: 123, }).End() ``` ### 复杂 请求 (设置 header 等) ``` request := gorequest.New() resp, body, errs := request.Get("http://example.com"). RedirectPolicy(redirectPolicyFunc). Set("If-None-Match", `W/"wyzzy"`). End() ``` ## JSON ### json为字符串 ``` request := gorequest.New() resp, body, errs := request.Post("http://example.com"). Set("Notes","gorequst is coming!"). Send(`{"name":"backy", "species":"dog"}`). End() ``` ### json 为 结构体与字符串混用 ``` type BrowserVersionSupport struct { Chrome string Firefox string } ver := BrowserVersionSupport{ Chrome: "37.0.2041.6", Firefox: "30.0" } request := gorequest.New() resp, body, errs := request.Post("http://version.com/update"). Send(ver). Send(`{"Safari":"5.1.10"}`). End() ``` ## 回调函数 ``` func printStatus(resp gorequest.Response, body string, errs []error){ fmt.Println(resp.Status) } gorequest.New().Get("http://example.com").End(printStatus) ``` ## Multipart/Form-Data ### 发送文字 ``` gorequest.New().Post("http://example.com/"). Type("multipart"). Send(`{"query1":"test"}`). End() ``` ### 发送文件 ``` f, _ := filepath.Abs("./file2.txt") bytesOfFile, _ := ioutil.ReadFile(f) gorequest.New().Post("http://example.com/"). Type("multipart"). SendFile("./file1.txt"). SendFile(bytesOfFile, "file2.txt", "my_file_fieldname"). End() ``` ## Basic Authentication ``` request := gorequest.New().SetBasicAuth("username", "password") resp, body, errs := request.Get("http://example-proxy.com").End() ``` ### 超时 ``` request := gorequest.New().Timeout(2*time.Millisecond) resp, body, errs:= request.Get("http://example.com").End() ``` ### EndBytes / EndStruct 返回 Bytes或结构体 `resp, bodyBytes, errs := gorequest.New().Get("http://example.com/").EndBytes() ` ``` heyYou struct { Hey string `json:"hey"` } var heyYou heyYou resp, _, errs := gorequest.New().Get("http://example.com/").EndStruct(&heyYou) ``` ## 重连多次,知道出现结果 ``` request := gorequest.New() resp, body, errs := request.Get("http://example.com/"). Retry(3, 5 * time.Second, http.StatusBadRequest, http.StatusInternalServerError). End() ``` ## 重定向 ``` request := gorequest.New() resp, body, errs := request.Get("http://example.com/"). RedirectPolicy(func(req Request, via []*Request) error { if req.URL.Scheme != "https" { return http.ErrUseLastResponse } }). End() ``` 当请求不是 https 是 取消发送 ## Debug 通过环境变量设置 ``` os.Setenv("GOREQUEST_DEBUG","0") // 0/1 开启/关闭 ``` 单个请求设置 ``` var buf bytes.Buffer logger := log.New(&buf, "[gorequest]", log.LstdFlags) request := gorequest.New().SetDebug(true).SetLogger(logger) ```