草庐IT

gorequest 包 : check specifically for timeout

coder 2024-07-13 原文

我正在使用以下包发出出站 http 请求 https://github.com/parnurzeal/gorequest

例如,我正在发出如下所示的 GET 请求 res, body, errs = goReq.Get(url).End()

我的问题是如何判断请求是否超时。

最佳答案

由于超时方法sets the dealines for dial, read, and write , 你可以使用 os.IsTimeout (net 和 net/url 包中的所有错误类型都实现了 Timeout() bool)。 gorequest 不支持上下文,所以 context.Canceled不必考虑:

package main

import (
    "log"
    "net/http"
    "net/http/httptest"
    "time"

    "github.com/parnurzeal/gorequest"
)

func main() {
    s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        time.Sleep(time.Second)
    }))

    request := gorequest.New()
    _, _, errs := request.Get(s.URL).Timeout(500 * time.Millisecond).End()

    for _, err := range errs {
        if os.IsTimeout(err) {
            log.Fatal("timeout")
        }
    }
}

关于gorequest 包 : check specifically for timeout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52379379/

有关gorequest 包 : check specifically for timeout的更多相关文章

  1. go - 如何使用gorequest发送二进制数据 - 2

    我正在尝试通过gorequestPUT方法发送html文件的内容。在我尝试联系的服务文档中提到,正文类型应该是Content-Type:application/octet-stream.当我执行时:req.Send(string(content))其中内容是字节slice([]byte),我的html文件已损坏,因为文件的内容已编码,所有空格、等特殊字符都被替换了。当我执行时:req.Send(content)我看到发送了以下内容:[60,104,116,109,....]这不是我所期望的。你能告诉我如何使用gorequest将html文件作为字节流传输到web服务吗?

  2. gorequest 包 : check specifically for timeout - 2

    我正在使用以下包发出出站http请求https://github.com/parnurzeal/gorequest例如,我正在发出如下所示的GET请求res,body,errs=goReq.Get(url).End()我的问题是如何判断请求是否超时。 最佳答案 由于超时方法setsthedealinesfordial,read,andwrite,你可以使用os.IsTimeout(net和net/url包中的所有错误类型都实现了Timeout()bool)。gorequest不支持上下文,所以context.Canceled不必考虑

  3. http - 如何使用 gorequest 发起 POST 请求 - 2

    Reddit有一个Oauth2的API端点,我需要在其中使用适当的header和数据执行POST以获得访问token。这是我的代码:packagemainimport("github.com/parnurzeal/gorequest""fmt")funcmain(){app_key:="K...A"app_secret:="3...M"ua_string:="script:bast:0.1(by/u/a...h)"username:="a...h"password:="..."r:=gorequest.New().SetBasicAuth(app_key,app_secret).Set

  4. go - 如何使用返回自身以进行链接的方法为复杂的 http 客户端(如 gorequest)编写接口(interface) - 2

    我正在编写一个包,它需要将*gorequest.SuperAgent的实例传递给子包中的方法//main.gofuncmain(){req:=gorequest.New()result:=subpackage.Method(req)fmt.Println(result)}//subpackage.gofuncMethod(req*gorequest.SuperAgent)string{req.Get("http://www.foo.com").Set("bar","baz")_,body,_:=req.End()returnbody}我一直在兜圈子试图为gorequestsuperag

随机推荐