POST request in PowerShell with InvokeRestMethod

Welcome Forums General PowerShell Q&A POST request in PowerShell with InvokeRestMethod

Viewing 1 reply thread
  • Author
    Posts
    • #231424
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      I Want to make a Post request in InvokeRestMethod.

      Below is the body parameter i used to post content to Body

      Expected parameter type is string for databaseName,password,roleName,databaseName.
      Expected parameter type is stringarray for roles
      My declaration for roles is not in stringarray type , it is being passed as json type i guess. please help in resolving below errors
        $body = @{
                databaseName=’admin’
                password= ‘changeme123’
                roles =   {databaseName=”sales”;roleName=”read”} | Out-String
                username= ‘sampleuser’
                }
      $json = $body | ConvertTo-Json
      Invoke-RestMethod -Method POST -Uri $getProjectUri -Headers @{Authorization = “Basic $base64AuthInfo”} -Credential $credential  -ContentType “application/json” -Body $json
      Error: Invoke-RestMethod : {“detail”:”Received JSON for the roles attribute does not match expected
      format.”,”error”:400,”errorCode”:”INVALID_JSON_ATTRIBUTE”,”parameters”:[“roles”],”reason”:”Bad Request”}
      At C:\Users\xxxxxx\powershellscript\getusers2.ps1:40 char:3
      + Invoke-RestMethod -Method POST -Uri $getProjectUri -Headers @{Autho …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

       

    • #231427
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      I want to add users to database with specific roles , and am trying to convert below code from curl into InvokeRestMethod.

      --request POST "https://cloud.mongodb.com/api/atlas/v1.0/groups/{PROJECT-ID}/databaseUsers" \
           --data '
             {
               "databaseName": "admin",
               "password": "changeme123",
               "roles": [{
                 "databaseName": "sales",
                 "roleName": "readWrite"
               }, {
                 "databaseName": "marketing",
                 "roleName": "read"
               }],
               "username": "david"
             }'
Viewing 1 reply thread
  • You must be logged in to reply to this topic.