欢迎各位小伙伴来到golang学习网,相聚于此都是缘哈哈哈!今天我给大家带来《使用 Openshift 服务进行连续 API 调用》,这篇文章主要讲到等等知识,如果你对Golang相关的知识非常感兴趣或者正在自学,都可以关注我,我会持续更新相关文章!当然,有什么建议也欢迎在评论留言提出!一起学习!
我正在尝试对 openshift 服务进行两部分 api 调用。第一个调用使我能够获取我们正在创建的 pod 的模板数据,第二个调用将调用 post 命令,该命令利用有效负载中 get 的结果,如下所示。 get 返回一个大的 json 对象。当我从 get 命令获取结果时,我将结果写入 ioutil.readall() 中,效果很好。然后,我使用 string() 转换为字符串,并尝试将其用作“模板”的字符串参数。我面临的问题是字符串参数包含转义字符。即 "{\"kind\":\"template\",\"apiversion\":\"template.openshift.io/v1\"...
我可以使用 bash 脚本来完成这项工作,但试图消除对代码中脚本的调用。
openshift 提供的 bash 脚本示例
$ curl -k \
-x post \
-d @- \
-h "authorization: bearer $token" \
-h 'accept: application/json' \
-h 'content-type: application/json' \
https://$endpoint/apis/template.openshift.io/v1/namespaces/$namespace/templateinstances <<eof
{
"kind": "templateinstance",
"apiversion": "template.openshift.io/v1",
"metadata": {
"name": "templateinstance"
},
"spec": {
"secret": {
"name": "secret"
},
"template": $(curl -k \
-h "authorization: bearer $token" \
-h 'accept: application/json' \
https://$endpoint/apis/template.openshift.io/v1/namespaces/openshift/templates/cakephp-mysql-example)
}
}
eof
go 代码示例...
func main() {
b, res := getTemplateData()
if !b{
fmt.Println(res)
os.Exit(1)
}else{
createTemplate(res)
}
}
type templateInstance struct{
Kind string `json:"kind"`
ApiVersion string `json:"apiVersion"`
Metadata MetaData `json:"metadata"`
Spec spec `json:"spec"`
}
type spec struct {
Secret Tsecret `json:"secret"`
Template string `json:"template"`
}
type Tsecret struct{
Name string `json:"name"`
}
func createTemplate(t string) {
var md MetaData
var sp spec
var ts Tsecret
md.Name = "template_name"
ts.Name = "secret_name"
sp.Secret = ts
sp.Template = t
var ti = templateInstance {
Kind: "TemplateInstance",
ApiVersion: "template.openshift.io/v1",
Metadata: md,
Spec: sp,
}
templateData, _ := json.Marshal(ti)
fmt.Println(string(templateData))
}
func getTemplateData() (bool, string){
URL := "https://example.com/apis/template.openshift.io/v1/namespaces/mynamespace/templates/mytemplate"
client := &http.Client{
Timeout:5*time.Second,
Transport: &http.Transport{
Proxy:nil,
TLSClientConfig: &tls.Config{InsecureSkipVerify:true},
},
}
req, err := http.NewRequest("GET", URL, nil)
if err != nil{
fmt.Println(err)
os.Exit(0)
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", myToken)
req.Header.Add("cache-control", "no-cache")
res, err := client.Do(req)
if err != nil{
fmt.Println("client.Do error")
fmt.Println(err)
return false, "Error calling get template"
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil{
return false, "Error reading Template result"
}else{
return true, string(body)
//string(body) gets used as the string value for "template" parameter of the payload in the bash script example...
}
}解决方案
额外的转义是将 json 文档编码为 json 的结果。
使用 json.RawMessage 将 json 模板按原样传递到输出。
type spec struct {
Secret Tsecret `json:"secret"`
Template json.RawMessage `json:"template"`
}
...
sp.Template = json.RawMessage(t)本篇关于《使用 Openshift 服务进行连续 API 调用》的介绍就到此结束啦,但是学无止境,想要了解学习更多关于Golang的相关知识,请关注golang学习网公众号!
本文内容来源于互联网,如有侵权请联系删除。
