用Golang的网络/ http标头碎片
作为我为与Jenkins RESTful API(https://wiki.jenkins-ci.org/display/JENKINS/Remote+access+API)交互而编写的Golang代码的一部分,我添加了crumb CSRF保护支持。
结构:
type Crumb struct {
Crumb string `json:"crumb"`
CrumbRequestField string `json:"crumbRequestField"`
}
码
...
crb := Crumb{}
// did some work to jsonify the crumb to Golang struct
// https://jenkins.mydomain.com/crumbIssuer/api/json
...
if (crb.Crumb != "" && crb.CrumbRequestField != "" ) {
req.Header.Set(crb.CrumbRequestField, crb.Crumb)
}
req.SetBasicAuth(jenkins.auth.Username, jenkins.auth.ApiToken)
// i think the issue is related to DefaultClient, but not sure
return http.DefaultClient.Do(req)
但是,上面的Golang实现继续获得403,但同样的事情与cURL调用工作正常。
&{403 No valid crumb was included in the request 403 HTTP/1.1 1 1 map[Server:[nginx] Date:[Thu, 31 Jul 2014 05:58:52 GMT] Content-Type:[text/html;charset=ISO-8859-1] Connection:[keep-alive] Cache-Control:[must-revalidate,no-cache,no-store]] 0xc20800ff80 -1 [chunked] false map[] 0xc2082ba270 0xc208005da0}
cURL日志:
> POST /computer/node1/toggleOffline HTTP/1.1
> Authorization: Basic <key>
> User-Agent: curl/7.30.0
> Host: jenkins.mydomain.com
> Accept: */*
> .crumb: 0d6401898751f250ff1f95b5bf9589db
我在这里回答我自己的问题。 我发现我的问题,它与Golang的net / http lib没有任何关系。 Jenkins crumb GET请求需要提供基本认证才能获得正确的crumb。
链接地址: http://www.djcxy.com/p/45435.html上一篇: Crumb with Golang's net/http header
下一篇: ReST: http 204 status code for polling for a resource after a 201 Created