【发布时间】:2021-10-25 01:06:53
【问题描述】:
我目前正在尝试为 API 调用准备一个 JSON 主体,它应该看起来像这样
curl -XPOST -H 'Authorization: Bearer ***API*KEY***' -H 'Content-Type: application/json' http://127.0.0.1:9000/api/alert -d '{
"title": "Other alert",
"description": "alert description",
"type": "external",
"source": "instance1",
"sourceRef": "alert-ref",
"severity": 3,
"tlp": 3,
"artifacts": [
{ "dataType": "ip", "data": "127.0.0.1", "message": "localhost" },
{ "dataType": "domain", "data": "thehive-project.org", "tags": ["home", "TheHive"] },
],
"caseTemplate": "external-alert"
}'
但是问题是我用 powershell 创建的 json 正文有奇怪的字符,看不出问题出在哪里。这是我的 JSON 正文
{
"tlp": 1,
"source": "Test",
"title": "Test Alert1",
"artifacts": "{\r\n \"dataType\": \"ip\",\r\n \"data\": \"127.0.0.1\"\r\n}",
"type": "external",
"sourceRef": "1",
"description": "Test",
"severity": 1
}
我按如下方式创建这个 JSON 主体。我已经尝试过 CustomObjects 和 Hashtables,但总是得到相同的输出
$body = @{
title = "$title"
description = "$Alert_Description"
type ="external"
source ="$Source"
sourceRef ="$SourceRef"
severity = $Severity
tlp = $tlp
$artifacts = [PSCustomObject]@{
dataType=ip
data=127.0.0.1}
}| ConvertTo-Json
$JsonBody = $body | ConvertTo-Json
有人可以给我一个提示吗?我已经不知道了
【问题讨论】:
-
为什么要转换成 json 两次?
$body已经是 JSON 字符串了。 -
@AdminOfThings 我认为嵌套的 Json 可能会有所帮助.. 但事实并非如此
标签: json powershell