digest认证,获取body为nil,怎么能获取到数据
resp, err := client.R().
SetDigestAuth("name”."password”).
SetBodyJsonMarshal(&Register).
SetSuccessResult(&rkBack).
Post("http://1.1.1.1/Register")
rkBack 为nil 实际抓包,是有数据返回的
检查下 err 是否为 nil,也开下 DevMode看看实际返回的跟rkBack结构是否一致
err不为nil,err的判断里面我加了continue,后面代码继续在跑,而且statuscode是200,也是成功,就是resp.body是空的 rkBack是struct的默认值,resp判断是nil DevMode 同样无法同样无法看到resp的内容
我开始用body一个int值作判断的,0就是成功,跑了好多天了,才发现这个问题
判断 err 是基础操作,如果不为 nil 一定要报出来方便排查
说错,err为nil 我做了判断 if err != nil { fmt.Println("reg", err) time.Sleep(3 * time.Second) registerch <- 1 continue } 程序继续往下执行了
digest两次连接,我感觉resp保留的是第一次401的空body,想看源代码,几次跳转.放弃,跟大佬求助
qq邮箱,已经发送
op解决了吗?同样的问题,首次请求成功最终拿到的resp是空的,抓包数据正常。(首次请求内部分为2步,401+200)。 第二次以及之后的每一次请求则都能成功,resp正常有数据的。(Connection: keep-alive,请求内部只有一步,不再有401,直接是200)。
因为我的应用比较简单,就是简单restful,除了一个digest认证,目前用原始net/http了,
又一次来试试,发现digest 获取的还是一次digest验证的时候的错误 package main
import ( "fmt"
"github.com/imroc/req/v3"
)
func main() { client := req.C().DevMode()
resp, err := client.R().
SetDigestAuth("admin", "a12345678").
Get("http://192.168.1.244/ISAPI/ContentMgmt/InputProxy/channels")
if err != nil {
fmt.Println(err)
}
body := resp.String()
fmt.Println("body:", body)
} body打印的还是第一次交互时候的body
上图是postman的交互
下面是的测试包 package main
import ( "github.com/imroc/req/v3" )
func main() { client := req.C().DevMode()
client.R().
SetDigestAuth("admin", "a12345678").
Get("http://192.168.1.244/ISAPI/ContentMgmt/InputProxy/channels")
} 输出是
PS C:\Users\Administrator\go\src\test> go run .\main.go 2025/06/15 21:37:55.510061 DEBUG [req] HTTP/1.1 GET http://192.168.1.244/ISAPI/ContentMgmt/InputProxy/channels GET /ISAPI/ContentMgmt/InputProxy/channels HTTP/1.1 Host: 192.168.1.244 User-Agent: req/v3 (https://github.com/imroc/req) Accept-Encoding: gzip
HTTP/1.1 401 Unauthorized Vary: Accept-Encoding X-Frame-Options: SAMEORIGIN Content-Type: text/html X-Content-Type-Options: nosniff Date: Sun, 15 Jun 2025 21:37:55 GMT Cache-Control: no-cache Content-Length: 229 X-XSS-Protection: 1; mode=block Connection: Keep-Alive WWW-Authenticate: Digest realm="640dc090e5db4d38c2543442", domain="/", qop="auth", nonce="8afd5309795be2ee:640dc090e5db4d38c2543442:1977587117a:1", opaque="799d5", algorithm="MD5", stale="FALSE" Accept-Ranges: bytes
<!DOCTYPE html>
<head>
<title>Unauthorized</title>
<link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon">
</head>
<body>
<h2>Access Error: 401 -- Unauthorized</h2>
<pre></pre>
</body>
</html>
2025/06/15 21:37:55.524749 DEBUG [req] HTTP/1.1 GET http://192.168.1.244/ISAPI/ContentMgmt/InputProxy/channels GET /ISAPI/ContentMgmt/InputProxy/channels HTTP/1.1 Host: 192.168.1.244 User-Agent: req/v3 (https://github.com/imroc/req) Authorization: Digest username="admin", realm="640dc090e5db4d38c2543442", nonce="8afd5309795be2ee:640dc090e5db4d38c2543442:1977587117a:1", uri="/ISAPI/ContentMgmt/InputProxy/channels", algorithm=MD5, cnonce="a39edb3b3ee34096", opaque="799d5", qop=auth, nc=00000001, response="4d58a043607543c4476deaa1029a4c24" Accept-Encoding: gzip
HTTP/1.1 200 OK Vary: Accept-Encoding X-Frame-Options: SAMEORIGIN Content-Type: application/xml; charset="UTF-8" X-Content-Type-Options: nosniff Date: Sun, 15 Jun 2025 21:37:55 GMT Content-Length: 2575 X-XSS-Protection: 1; mode=block Connection: Keep-Alive Accept-Ranges: bytes
PS C:\Users\Administrator\go\src\test>
这个是海康录像机isapi接口获取录像机通道的接口,digest两次交互和普通交互不同的是海康录像机第一次报错 body会输出一个xml,我怎么能获取验证成功后正确body
上图是postman的交互
下面是的测试包 package main
import ( "github.com/imroc/req/v3" )
func main() { client := req.C().DevMode()
client.R(). SetDigestAuth("admin", "a12345678"). Get("http://192.168.1.244/ISAPI/ContentMgmt/InputProxy/channels")} 输出是
PS C:\Users\Administrator\go\src\test> go run .\main.go 2025/06/15 21:37:55.510061 DEBUG [req] HTTP/1.1 GET http://192.168.1.244/ISAPI/ContentMgmt/InputProxy/channels GET /ISAPI/ContentMgmt/InputProxy/channels HTTP/1.1 Host: 192.168.1.244 User-Agent: req/v3 (https://github.com/imroc/req) Accept-Encoding: gzip
HTTP/1.1 401 Unauthorized Vary: Accept-Encoding X-Frame-Options: SAMEORIGIN Content-Type: text/html X-Content-Type-Options: nosniff Date: Sun, 15 Jun 2025 21:37:55 GMT Cache-Control: no-cache Content-Length: 229 X-XSS-Protection: 1; mode=block Connection: Keep-Alive WWW-Authenticate: Digest realm="640dc090e5db4d38c2543442", domain="/", qop="auth", nonce="8afd5309795be2ee:640dc090e5db4d38c2543442:1977587117a:1", opaque="799d5", algorithm="MD5", stale="FALSE" Accept-Ranges: bytes
<!DOCTYPE html> <head> <title>Unauthorized</title> <link rel="shortcut icon" href="data:image/x-icon;," type="image/x-icon"> </head> <body> <h2>Access Error: 401 -- Unauthorized</h2> <pre></pre> </body> </html>2025/06/15 21:37:55.524749 DEBUG [req] HTTP/1.1 GET http://192.168.1.244/ISAPI/ContentMgmt/InputProxy/channels GET /ISAPI/ContentMgmt/InputProxy/channels HTTP/1.1 Host: 192.168.1.244 User-Agent: req/v3 (https://github.com/imroc/req) Authorization: Digest username="admin", realm="640dc090e5db4d38c2543442", nonce="8afd5309795be2ee:640dc090e5db4d38c2543442:1977587117a:1", uri="/ISAPI/ContentMgmt/InputProxy/channels", algorithm=MD5, cnonce="a39edb3b3ee34096", opaque="799d5", qop=auth, nc=00000001, response="4d58a043607543c4476deaa1029a4c24" Accept-Encoding: gzip
HTTP/1.1 200 OK Vary: Accept-Encoding X-Frame-Options: SAMEORIGIN Content-Type: application/xml; charset="UTF-8" X-Content-Type-Options: nosniff Date: Sun, 15 Jun 2025 21:37:55 GMT Content-Length: 2575 X-XSS-Protection: 1; mode=block Connection: Keep-Alive Accept-Ranges: bytes
PS C:\Users\Administrator\go\src\test>
这个是海康录像机isapi接口获取录像机通道的接口,digest两次交互和普通交互不同的是海康录像机第一次报错 body会输出一个xml,我怎么能获取验证成功后正确body
resp.Body() 或 resp.String() 输出的就是验证成功的 body,如果没有,可能是服务端本身没响应 body 内容。可以用 httpbin.org 验证:
package main
import (
"fmt"
"github.com/imroc/req/v3"
)
func main() {
client := req.C().SetCommonDigestAuth("admin", "123456").DevMode()
resp := client.R().MustGet("https://httpbin.org/digest-auth/auth/admin/123456")
fmt.Printf("body:\n%s", resp.String())
}
谢谢
client.R().SetDigestAuth 这个函数插件提示已经弃用 而且获取不到body,使用SetCommonDigestAuth,可以,但是如果我不想设置客户端级别的认证,我对单个网站做认证,应该怎么做
client.R().SetDigestAuth 这个函数插件提示已经弃用 而且获取不到body,使用SetCommonDigestAuth,可以,但是如果我不想设置客户端级别的认证,我对单个网站做认证,应该怎么做
可以针对指定网站专门用一个 client
谢谢