xspyboyx
xspyboyx

Reputation: 35

Format for json via powershell

I'm trying to use powershell to send json to DevOps API. I can't seem to figure out how to properly format this so powershell will take it. I keep getting this error. Any advice? I'm able to use the same json in Postman without any issues. Thanks

$URI= "https://vsaex.dev.azure.com/$ClientOrg/_apis/userentitlements?api-version=5.1-preview.2" $AzureDevOpsAuthenicationHeader = @{Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($PAT)")) }

Invoke-RestMethod -uri $URI -Method POST -Headers $AzureDevOpsAuthenicationHeader -Body $a -ContentType "application/json"


$a= ConvertFrom-JSON @'
{
  "accessLevel": {
      "licensingSource": "msdn",
      "accountLicenseType": "enterprise",
      "msdnLicenseType": "enterprise"
  },
  "extensions": [
    {
      "id": "ms.feed" 
    }
  ],
  "user": {
    "principalName": "[email protected]",
    "subjectKind": "user"
  },
  "projectEntitlements": [
    {
      "group": {
        "groupType": "projectAdministrator"
      },
      "projectRef": {
        "id": "0685a10e"
      }
    }
   ]
  }
'@



Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: userEntitlement","typeName":"System.ArgumentNullException, 
mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
At line:4 char:1
+ Invoke-RestMethod -uri $URI -Method POST -Headers $AzureDevOpsAutheni ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Upvotes: 2

Views: 216

Answers (1)

JPBlanc
JPBlanc

Reputation: 72610

Your body should be the JSON string itself and not an object built from the JSON.

$a= @'
{
  "accessLevel": {
      "licensingSource": "msdn",
      "accountLicenseType": "enterprise",
      "msdnLicenseType": "enterprise"
  },
  "extensions": [
    {
      "id": "ms.feed" 
    }
  ],
  "user": {
    "principalName": "[email protected]",
    "subjectKind": "user"
  },
  "projectEntitlements": [
    {
      "group": {
        "groupType": "projectAdministrator"
      },
      "projectRef": {
        "id": "0685a10e"
      }
    }
   ]
  }
'@

Invoke-RestMethod -uri $URI -Method POST -Headers $AzureDevOpsAuthenicationHeader -Body $a -ContentType "application/json"

Upvotes: 1

Related Questions