0

我想通过一个任务传递一个 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", "type":"team"},
        {"id":"bb4d9938-c3c2-455d-aaab-727aa701c0d8", "type":"user"},
        {"username":"trinity@opsgenie.com", "type":"user"},
        {"id":"aee8a0de-c80f-4515-a232-501c0bc9d715", "type":"escalation"},
        {"name":"Nightwatch Escalation", "type":"escalation"},
        {"id":"80564037-1984-4f38-b98e-8a1f662df552", "type":"schedule"},
        {"name":"First Responders Schedule", "type":"schedule"}
    ],
    "visibleTo":[
        {"id":"4513b7ea-3b91-438f-b7e4-e3e54af9147c","type":"team"},
        {"name":"rocket_team","type":"team"},
        {"id":"bb4d9938-c3c2-455d-aaab-727aa701c0d8","type":"user"},
        {"username":"trinity@opsgenie.com","type":"user"}
    ],
    "actions": ["Restart", "AnExampleAction"],
    "tags": ["OverwriteQuietHours","Critical"],
    "details":{"key1":"value1","key2":"value2"},
    "entity":"An example entity",
    "priority":"P1"
  
  }")
)
4

1 回答 1

0

在这种情况下,解决我的实际问题是我应该传递一个实际的 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"
  
  })
)
于 2020-11-06T07:24:05.413 回答