[英]JSON: cannot unmarshal string
我正在嘗試使用 Rest API,但是當我查詢時,我收到以下我不明白的錯誤,我試圖在這里查看類似的問題,但它們是非常特殊的情況。 這是我使用的代碼
$oAuth2TokenUrl = "https://api.cloudflare.com/client/v4/graphql"
$accessKey = '1234'
$Cloudflare_Oauth_Header = @{
"X-AUTH-EMAIL" = "bryan";
"X-AUTH-KEY" = "$accessKey";
"Content-type" = "application/json"
}
$query = '\"query\": \"{
viewer {
zones(filter: {zoneTag: \"1234\"}) {
httpRequests1mGroups(orderBy: [datetimeMinute_ASC], limit: 1000, filter: {datetime_geq: \"2020-05-07T10:00:00Z\", datetime_lt: \"2020-05-07T10:10:00Z\"}) {
dimensions {
datetimeMinute
}
sum {
browserMap {
pageViews
uaBrowserFamily
}
}
uniq {
uniques
}
}
}
}
} \"' | ConvertTo-Json
$Cloudflare_zone = Invoke-RestMethod -Uri $oAuth2TokenUrl -Method POST -Headers $Cloudflare_Oauth_Header -Body $query -ContentType 'application/json'
因此,一旦我檢查了$Cloudflare_zone
的內容,就會打印以下內容
{@{message=failed to recognize JSON request: 'json: cannot unmarshal string into Go value of type graphql.httpPostBody'; path=; extensions=}}
錯誤顯示“json: cannot unmarshal string into graphql.httpPostBody 類型的 Go 值” 。
這歸結為“無法解析您發送的 JSON 字符串”。 果然,您的 JSON 字符串無效。
拆分后處理起來會容易得多:帶有 GraphQL 的字符串變量,以及帶有請求正文的 PowerShell 哈希表。 然后可以將 hash 表轉換為 JSON 進行傳輸。
$graphQL = '{
# whatever (note the single quotes, so any double quotes in here can stay unescaped)
}'
$header = @{
"X-AUTH-EMAIL": "bryan"
"X-AUTH-KEY": "$accessKey"
}
$body = @{
query: $graphQL
# potentially other parameters that are not part of the GraphQL
}
$Cloudflare_zone = Invoke-RestMethod `
-Uri "https://api.cloudflare.com/client/v4/graphql" `
-Method POST `
-Headers $Cloudflare_Oauth_Header `
-Body ($body | ConvertTo-Json) `
-ContentType 'application/json; charset=utf-8'
筆記:
Invoke-RestMethod
為所欲為。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.