首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用转换-json和调用curl的Powershell不喜欢字段中的空格。

使用转换-json和调用curl的Powershell不喜欢字段中的空格。
EN

Stack Overflow用户
提问于 2018-04-10 15:53:53
回答 1查看 503关注 0票数 1

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

目前,我正在尝试获取curl命令,以便将一些信息写入在我的计算机上本地运行的elasticsearch中。

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

我遇到的问题是,任何有空格的字段都会立即被卷曲抛出,出现意外的输入结束错误,删除字段数据中的空间可以让它很好地运行。

代码语言:javascript
运行
复制
    $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更改为“测试spaces.txt”将生成错误。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-10 16:58:40

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

代码语言:javascript
运行
复制
$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\"}"

您应该尝试做一些列出以下内容的事情:

代码语言:javascript
运行
复制
$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对它们进行格式化。它应该正确地处理字符串中的空格。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49758040

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档