1

I'm trying to write a powershell script (powershell 6 and 7) to make REST call API from a text file with JSON body. Here's my code:

$uri = 'URI'
$json = Get-Content 'TXT_FILE_WITH_JSON_BODY' | Out-String | ConvertFrom-Json | ConvertTo-Json | %{[regex]::Unescape($_)}
$username = 'USER'
$password = 'PASSWORD'
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))
Invoke-RestMethod -Method POST -SkipCertificateCheck -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} $uri -body $json -ContentType "application/json"

My txt file:

'{
  "apiversion": "3.0",
  "process": {
    "profileId": "ID",
    "jobType": "TYPE",
    "origin": "ORIGIN",
    "processWhileGrowing": true
  },
  "jobSource": {
    "sources": [
      {
        "uri": "\\\\\\\\10.22.6.250\\\\share\\\\test\\\\TEST01.mov"
      }
    ],
    "metadata": [
      {
        "name": "Description",
        "value": "Bla bla bla"
      },
      {
        "name": "title",
        "value": "Title"
      },
      {
        "name": "channel",
        "value": "SRV1"
      },
      ,
      {
        "name": "type",
        "value": "TYPE01"
      },
      {
        "name": "source",
        "value": "SOURCE01"
      }
    ]
    }
  }
  '

Then when I execute my script, I get this:

PS F:\Desktop> .\test01.ps1
Invoke-RestMethod: F:\Desktop\test01.ps1:6
Line |
   6 |  Invoke-RestMethod -Method POST -SkipCertificateCheck -Headers @{Autho …
     |  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | {
  "status": "HTTP_ERROR",
  "message": "SERVER_ERROR",
  "message_detail": null
}

My first idea is that my REST API does not respond well to the quotation at the start and end of my txt file (json body). However, if I remove the quotes from my txt file, Powershell say the following:

PS C:\Users\Brice> F:\Desktop\jsonconvert.ps1
ConvertFrom-Json : Invalid JSON primitive: .
At F:\Desktop\jsonconvert.ps1:1 char:63
+ ... nt 'F:\Desktop\1.txt' | Out-String | ConvertFrom-Json | Conve ...
+                                                  ~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

Any idea how can I fix this ?

Brice
  • 67
  • 2
  • 13
  • You *do* need to remove the single quotes surrounding the entire json file, but you've also got a dangling comma on it's own line in your ```metadata``` section, just after the ```"value": "SRV1"``` - remove that and it should parse the json ok. You can find problems like this fairly easily if you open the file in a json-aware text editor / IDE that has syntax highlighting... – mclayton Sep 21 '20 at 19:55
  • Thanks for your answer, I actually saw the mistakes right after posting and did remove the extra comma but I didn't work any better. Here's the new txt file without metadata field, extra comma and simple quotes: [link](https://pastebin.com/vFjhLxtp) Now my issue is that the conversion from json txt file doesn't work well in Powershell. My body looks like this: [link](https://pastebin.com/74NVidUd) you can see that the format is not right at the line 11... – Brice Sep 21 '20 at 20:04
  • So apparently my issue is my brackets in the TXT file, Powershell seems to convert them and I need to keep them as is. – Brice Sep 21 '20 at 21:03
  • Fix it with ConvertTo-Json -Depth 5 – Brice Sep 21 '20 at 21:11
  • So this is resolved? – Doug Maurer Sep 22 '20 at 20:30
  • it is, mark as resolved now. Thanks – Brice Sep 23 '20 at 22:09

1 Answers1

0

Resolve by removing simple quotes in the txt file and added the following to my code ConvertTo-Json -Depth 5

Brice
  • 67
  • 2
  • 13