locked
Invoke-Webrequest issue RRS feed

  • Question

  • I have a frustrating issue. I am using Invoke-Webrequest in Powershell and posting data to a url. I am building the body array and then converting it to JSON before posting. But when it goes across it is adding /r/n characters to the data thus causing it to error out because it is not in the format they are expecting. This is what the vendor said they are seeing on their end:

    {
    '{\r\n    "firstName":  "John",
    \r\n    "lastName": "Smith"\r\n
    }

    If I display the data on my end before sending I do not see these characters:

    {
    "firstName":  "John",
    "lastName":  "Smith"
    }

    This is my code:

    $BodyUserInfo =
    [pscustomobject]@{
    firstName= $row.FirstName
    lastName= $row.LastName
    }
     
    $BodyUserInfo = ConvertTo-Json -InputObject $BodyUserInfo
     
    Write-Host $BodyUserInfo;

    $usercreateinfo = Invoke-WebRequest  'url' -Method POST -Body $BodyUserInfo -Headers @{'Authorization' = 'Bearer ' + $token} -ErrorVariable RespErr;

    What do I need to do to correct the issue?

    Thanks.

    Monday, January 20, 2020 2:51 PM

All replies

  • Where did that extra curly brace and single-quote come from????

    {
    '{\r\n    "firstName":  "John",
    \r\n    "lastName": "Smith"\r\n
    }


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)


    Monday, January 20, 2020 4:24 PM
  • I think I missed the ending '}' when I copied and pasted. The single-quote was probably manually added by the vendor when they sent me the sample (maybe to delineate the string). the main issue for me is where the /r/n came from.

    Monday, January 20, 2020 5:12 PM
  • Try this:

    $bodyuserinfo = $bodyuserinfo -replace "\r\n",''

    There's no need to pretty-print the info you're sending to another machine.

    If the single-quote was meant to distinguish what they saw, where's the closing single-quote?

    Also, the second \r\n is actually at the end of the "firstname" line, not at the beginning of the "lastname" line. Why is it shown where it is in their example?

    You might want to run you code from a machine where you can install a program like "Fiddler" and have a look at what's actually being sent as opposed to what someone else is telling you they received.

    FWIW, I put your data through a JSON validator. There's nothing wrong with it. All the formatter did was to remove the space after the colon on each line, but that's not really a problem.

    You may have a problem, but I don't think it's related to Powershell.


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, January 20, 2020 8:11 PM
  • I'd suggest you try specifying the content type as json in your Invoke-Webrequest call: -ContentType 'application/json'
    Monday, January 20, 2020 8:20 PM
  • Here is what is likely happening.  I tripped over it by accident:

    PS C:\scripts> $Body =
    >> [pscustomobject]@{
    >> firstName= $row.FirstName
    >> lastName= $row.LastName
    >> }
    PS C:\scripts> $B = ConvertTo-Json -InputObject $Body
    PS C:\scripts> $b
    {
        "firstName":  null,
        "lastName":  null
    }
    PS C:\scripts> $B = ConvertTo-Json -InputObject $B
    PS C:\scripts> $b
    "{\r\n    \"firstName\":  null,\r\n    \"lastName\":  null\r\n}"
    PS C:\scripts>
    
    

    Conversion of Json to Json causes the inclusion of the line terminators.

    This should prevent that:

    $obj = [pscustomobject]@{
     firstName= $row.FirstName
     lastName= $row.LastName
    }
    Remove-Variable json -ea 0
    $json = $obj | ConvertTo-Json
    

    It happens intermittently when the variable has already been used.  Might be a bug in the CmdLet or the Json classes.


    \_(ツ)_/

    Monday, January 20, 2020 9:15 PM
  • To quote Arte Johnson as "Wolfgang" on the old "Laugh-In" TV show, "Veeeeery interesting".

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Monday, January 20, 2020 10:28 PM
  • To quote Arte Johnson as "Wolfgang" on the old "Laugh-In" TV show, "Veeeeery interesting".

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Definitely has me baffled.  Veeeeeery baffled.

    \_(ツ)_/

    Monday, January 20, 2020 10:41 PM
  • The other thing that is strange is that the /r/n sequences are whitespace and are ignored by all Json parsers.


    \_(ツ)_/

    Monday, January 20, 2020 10:45 PM