问题描述
我想通过任务传递http.post json,但找不到有关如何转义字符串的文档,并且字符串中有引号
import "json"
import "http"
from(bucket: "prod")
|> range(start: v.timeRangeStart,stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "canalplus_subs_per_mode_hourly")
|> filter(fn: (r) => r["_field"] == "reach")
|> filter(fn: (r) => r["label"] == "Live")
|> aggregateWindow(every: 12h,fn: median,createEmpty: false)
|> yield(name: "median")
// -H "Content-Type: application/json"
// -H "Authorization: GenieKey eb243sasdasdasdasdsadassadasdsad"
http.post(
url: "https://api.opsgenie.com/v2/alerts",headers: { "Content-Type":"application/json",Authorization:"GenieKey eb243592-faa2-4ba2-a551q-1afdf565c889"},data: bytes(v: "{
"message": "An example alert message","alias": "Life is too short for no alias","description":"Every alert needs a description","responders":[
{"id":"4513b7ea-3b91-438f-b7e4-e3e54af9147c","type":"team"},{"name":"NOC",{"id":"bb4d9938-c3c2-455d-aaab-727aa701c0d8","type":"user"},{"username":"trinity@opsgenie.com",{"id":"aee8a0de-c80f-4515-a232-501c0bc9d715","type":"escalation"},{"name":"Nightwatch Escalation",{"id":"80564037-1984-4f38-b98e-8a1f662df552","type":"schedule"},{"name":"First Responders Schedule","type":"schedule"}
],"visibleto":[
{"id":"4513b7ea-3b91-438f-b7e4-e3e54af9147c",{"name":"rocket_team","type":"user"}
],"actions": ["Restart","AnExampleAction"],"tags": ["OverwriteQuietHours","Critical"],"details":{"key1":"value1","key2":"value2"},"entity":"An example entity","priority":"P1"
}")
)
解决方法
在这种情况下,解决了我实际问题的是我应该传递实际的json而不是字符串化的版本
import "json"
import "http"
from(bucket: "prod")
|> range(start: v.timeRangeStart,stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "canalplus_subs_per_mode_hourly")
|> filter(fn: (r) => r["_field"] == "reach")
|> filter(fn: (r) => r["label"] == "Live")
|> aggregateWindow(every: 12h,fn: median,createEmpty: false)
|> yield(name: "median")
http.post(
url: "https://api.opsgenie.com/v2/alerts",headers: { "Content-Type":"application/json",Authorization:"GenieKey 7"},data: json.encode(v: {
"message": "An example alert message 123"
})
)