1

I'm still having issues with getting json working with the curl command in powershell.

even a simple request to POST something into elastic fails miserably with the error

Unexpected character ('D' (code 68)): was expecting double-quote to start field name

I've stripped the script down to the basics just to try and test curl and json and still get failures

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

$elasticdata = @{
        timereceived = "test"
        timesent     = "testing"
        name         = "anon"
        status       = 0
    }

$curldata = $elasticdata | convertto-json -Compress

$elasticoutput = "h:\powershell\esb\elastic\elastic.txt"
$elastichost   = "http://localhost:9200/newtest20/filecopy/?pretty"
$elasticheader = "content-type: application/json"
$elamethod     = "POST"

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

& $curlexe @curlargs
  • Your sample data doesn't contain any upper case letter `D` which causes your error message - so difficult to guess what's not double quoted. I'd suggest you use a tool like `EchoArgs.exe` from pscx to see what get's passed to cURL –  Apr 17 '18 at 13:00
  • Have you tried using double quotes on your flags? – Christopher Apr 17 '18 at 13:10
  • if I type verbatim what is in $curldata but then escape out the " then it goes into the command fine `$curldata = '{\"timereceived\":\"test\",\"timsent\":\"testing\",\"name\":\"anon\",\"status\":0}'` – Ian Ramsden Apr 17 '18 at 13:19
  • @IanRamsden Yeah windows isn't a fan of those single quotes: https://stackoverflow.com/questions/31503754/errormessagesunexpected-character-code-39-expected-a-valid-value – Christopher Apr 17 '18 at 13:38

1 Answers1

0

If your server is running Powershell 2.0 you will not have Invoke-webRequestbut ConvertTo-Json will also be missing. I also encountered this issue in the past and I made these functions to workaround that issue

function Invoke-WebRequest([string] $Url, [string] $Method, $BodyObject)
{    
    $request = [System.Net.WebRequest]::Create($Url)
    $request.Method = $Method
    $request.ContentType = "application/json"

    if ($Method -eq "POST")
    {
        try
        {
            $body = ConvertTo-Json20 -InputObject $BodyObject
            $requestStream = $request.GetRequestStream()
            $streamWriter = New-Object System.IO.StreamWriter($requestStream)
            $streamWriter.Write($body)            
        }

        finally
        {
            if ($null -ne $streamWriter) { $streamWriter.Dispose() }
            if ($null -ne $requestStream) { $requestStream.Dispose() }
        }
    }

    $response = $request.GetResponse()    
    if ($response.StatusCode -ne [System.Net.HttpStatusCode]::OK)
    {
        throw "ERROR Could not $Method url [$Url]"
    }

    return $response
}

function ConvertTo-Json20($InputObject){
    Add-Type -Assembly System.Web.Extensions
    $jsonSerializer = New-Object System.Web.Script.Serialization.JavascriptSerializer

    return $jsonSerializer.Serialize($InputObject)
}
MonkeyDreamzzz
  • 3,978
  • 1
  • 39
  • 36
  • 1
    Agree with the tip on `Invoke-WebRequest`, but for the hashtable, I think the original was correct. The semi-colon is only needed if you define multiple items on one line. Adding them here with the items defined on separate lines makes no difference to the output. @Christopher, did that actually fix your problem? – boxdog Apr 17 '18 at 13:00
  • even changing the hash table with ; made no difference, i've looked at the output and it looks like json it just doesn't like to be pushed to the external curl command, I'm using curl as the powershell version running on the server doesn't have invoke-webrequest or invoke-rest – Ian Ramsden Apr 17 '18 at 13:01
  • thank you @rubanov for getting me over a hurdle I hadn't even reached yet! – Ian Ramsden Apr 17 '18 at 13:40