【问题标题】:Powershell using convertto-json and calling curl doesn't like spaces in fieldsPowershell 使用 convertto-json 并调用 curl 不喜欢字段中的空格
【发布时间】:2018-09-20 08:00:12
【问题描述】:

我正在使用从https://curl.haxx.se/下载的外部curl命令

我目前正在尝试让 curl 命令将一些信息写入在我的机器上本地运行的 elasticsearch

我正在通过 convertto-json 运行信息,以确保数据在传递给外部 curl 命令之前格式正确

我遇到的问题是,任何带有空格的字段都会立即被 curl 抛出,并出现意外的输入结束错误,删除字段数据中的空格可以让它正常运行。

    $curlExe = "h:\powershell\esb\elastic\curl\curl.exe"

    function global:elasticcall ([string] $elasticdata)
    {
        $elasticoutput = "h:\powershell\esb\elastic\elastic.txt"
        $elastichost="http://localhost:9200/newtest4/filecopy/?pretty"
        $elasticheader="content-type: application/json"
        $elamethod="POST"
        $jsonelasticdata = $elasticdata | ConvertTo-Json -Compress 

        $curlargs = $elastichost,
                    '-X',$elamethod,
                    '-d',$jsonelasticdata,
                    '-H',$elasticheader

        write-host "Curl arguments in the entire string : " $curlargs
              
        & $curlexe @curlargs

        $elasticdata | Out-File $elasticoutput -Append

    }

$timereceived="randomness"
$timesentconv="randomness2"
$name="testingspaces.txt"
$curlstatus=0
$elasticbody = '{"timereceived":"' + $timereceived + '","timesent":"' + $timesentconv + '","Filename":"' + $name + '","Status":"' + $curlstatus + '"}'

elasticcall $elasticbody

将变量 $name 更改为“testing spaces.txt”会产生错误。

【问题讨论】:

  • 你为什么不用Invoke-RestMethod
  • 因为invoke-restmethod在运行这个的服务器上不可用,powershell版本太低

标签: json powershell curl


【解决方案1】:

您正在使用您已经手动尝试格式化为 json 的当前字符串 $elasticbody

$timereceived = "randomness"
$timesentconv = "randomness2"
$name = "testing spaces.txt"
$curlstatus = 0
$elasticbody = '{"timereceived":"' + $timereceived + '","timesent":"' + $timesentconv + '","Filename":"' + $name + '","Status":"' + $curlstatus + '"}'
$elasticbody | ConvertTo-Json -Compress

这将返回以下内容,您可以看到文件名周围没有引号,并且字符串本身包含在引号中。

结果:"\"timereceived\":\"randomness\",\"timesent\":\"randomness2\",\"Filename\":\"testing spaces.txt\",\"Status\":\"0\"}"

你应该尝试做一些事情列出这个:

$GLOBAL:curlExe = "h:\powershell\esb\elastic\curl\curl.exe"

function global:elasticcall {
    param (
        $timereceived,
        $timesentconv,
        $name,
        $curlstatus,
        $elasticoutput = "h:\powershell\esb\elastic\elastic.txt",
        $elastichost = "http://localhost:9200/newtest4/filecopy/?pretty",
        $elasticheader = "content-type: application/json",
        $elamethod = "POST"
    )

    $elasticdata = @{
        timereceived = $timereceived
        timesent     = $timesentconv
        Filename     = $name
        Status       = $curlstatus
    }
    
    $jsonelasticdata = $elasticdata | ConvertTo-Json -Compress 

    $curlargs = $elastichost,
    '-X', $elamethod,
    '-d', $jsonelasticdata,
    '-H', $elasticheader

    write-host "Curl arguments in the entire string : " $curlargs

    & $curlexe @curlargs

    $elasticdata | Out-File $elasticoutput -Append

}

global:elasticcall -timereceived 'randomness' -timesentconv 'randomness2' -name 'testing spaces.txt' -curlstatus 0

您接受您的变量作为参数并使用函数内的ConvertTo-Json 对其进行格式化。它应该正确处理字符串中的空格。

【讨论】:

  • 谢谢你,我是 json 新手,这对我有很大帮助
猜你喜欢
  • 2017-11-26
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 2022-08-03
  • 2021-11-28
  • 2023-03-28
  • 1970-01-01
相关资源
最近更新 更多