invoke rest method in powershell

Welcome Forums General PowerShell Q&A invoke rest method in powershell

Viewing 5 reply threads
  • Author
    Posts
    • #231715
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      Hi All,

      I am trying to convert below curl coomand into powershell.

      curl –user “{PUBLIC-KEY}:{PRIVATE-KEY}” –digest \
      –header “Accept: application/json” \
      –header “Content-Type: application/json” \
      –include \
      –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”

      }’

      I converted above curl into Restmethod to execute in powershell

      $Json = ‘{“databaseName”:”admin”,”password”:”changeme123″,”username”:”sample”,”roles”:[{“databaseName”:”sales”,”roleName”:”read”,}]}’
      Invoke-RestMethod -Uri $getProjectUri -Body $Json -ContentType Application/Json -Headers @{Authorization = “Basic $base64AuthInfo”} -Credential $credential -Method Post
      Here is the Error Message :
      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\pik0097\powershellscript\a3.ps1:10 char:3
      + Invoke-RestMethod -Uri $getProjectUri -Body $Json -ContentType Appl …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
      My Analysis:
      I am passing -> “roles”:[{“databaseName”:”sales”,”roleName”:”read”,}] ->in below json variable,which is being passed as json but as per microsoft documentation , expected type for roles is string array
      roles.databaseName=sales , roles.roleName=read
      Please suggest
      $Json = ‘{“databaseName”:”admin”,”password”:”changeme123″,”username”:”sample”,”roles”:[{“databaseName”:”sales”,”roleName”:”read”,}]}’
    • #231727
      Participant
      Topics: 12
      Replies: 525
      Points: 1,233
      Helping Hand
      Rank: Community Hero

      My advise don’t try to format JSON manually in PowerShell. Build your data as hashtables and use Convertto-JSON as in:

      $DataToSend = @{
          databaseName = 'admin'
          password     = 'changeme123'
          username     = 'sample'
          roles        = @(
              @{
                  databaseName = 'sales'
                  roleName     = 'read'
              }        
              @{
                  databaseName = 'marketing'
                  roleName     = 'read'
              }
          )
      }
      
      $Json = $DataToSend | ConvertTo-Json
      
      $Json
      

      and your JSON object would look like:

      {
          "username":  "sample",
          "databaseName":  "admin",
          "password":  "changeme123",
          "roles":  [
                        {
                            "databaseName":  "sales",
                            "roleName":  "read"
                        },
                        {
                            "databaseName":  "marketing",
                            "roleName":  "read"
                        }
                    ]
      }
      
      • This reply was modified 1 month, 1 week ago by Sam Boutros.
    • #231739
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      Thank you so much @Sam boutrous , it is working , you made my day 🙂

    • #231745
      Participant
      Topics: 12
      Replies: 1643
      Points: 2,665
      Helping Hand
      Rank: Community Hero

      MongoDB is a pretty popular database, there is probably already pre-built powershell modules:

      PsMongo – A Powershell Module For MongoDB from PowerShell

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

      Hi All,

      I used below code to add user named sample for sales database with read access

      $DataToSend = @{
      databaseName = ‘admin’
      password = ‘changeme123’
      username = ‘sample’
      roles = @(
      @{
      databaseName = ‘sales’
      roleName = ‘read’
      }
      )
      }

      $Json = $DataToSend | ConvertTo-Json

      $Json

      If i have to append a new role for the same user “sample” for “marketing” database with readWrite access , can you please suggest. I tried below approaches and failed.

      1. with patch command I found that existing sales db is being replaced with marketing db instead of append/ adding marketing db to same user.
      2. With post command I see an error “username exists” , as user name “sample” already exists .

      What should be done if i have to add/append a new database along with role for an existing user with an existing db and role ,please provide suggestions .

      • This reply was modified 1 month, 1 week ago by pk30.
    • #232465
      Participant
      Topics: 12
      Replies: 1643
      Points: 2,665
      Helping Hand
      Rank: Community Hero

      That is a MongoDB question, specifically the api: https://api.mongodb.com/

Viewing 5 reply threads
  • You must be logged in to reply to this topic.