MongoDB Atlas API: Create New CustomRole With Curl & Payload

Dear MongoDB Atlas Team,

I am having issues creating customRoles in Mongodb Atlas using API and curl.

Resource - MongoDB Atlas Administration API

I can display customRoles created in MongoDB Atlas UI like this.

curl --user "{username}:{password}"  --digest --header "Accept: application/vnd.atlas.2023-02-01+json" https://cloud.mongodb.com/api/atlas/v2/groups/{groupID}/customDBRoles/roles
[{"actions":[{"action":"COLL_MOD","resources":[{"collection":"","db":"example"}]}],"inheritedRoles":[],"roleName":"callmodRole"},{"actions":[{"action":"COLL_MOD","resources":[{"collection":"","db":"example-logs"}]}],"inheritedRoles":[],"roleName":"customcallmodRole"}]

I am also able to delete

curl -v -X DELETE https://cloud.mongodb.com/api/atlas/v2/groups/{groupID}/customDBRoles/roles/customremoveRole --user "{username}:{password}"  --digest --header "Accept: application/vnd.atlas.2023-02-01+json"

The issue is I am unable to create customRoles in MongoDB Atlas with this curl command.

>curl -v -X POST https://cloud.mongodb.com/api/atlas/v2/groups/{groupID/customDBRoles/roles --user "{username}:{password}"  --digest --header "Accept: application/vnd.atlas.2023-02-01+json" -d {"actions":[{"action":"REMOVE","resources":[{"collection":"","db":"ambience"}]}],"inheritedRoles":[],"roleName":"customremoveRole"}

I got this error

 "error" : 415,
 "reason" : "Unsupported Media Type"

If I change the --header to “Content-Type: application/vnd.atlas.2023-02-01+json” or “Content-Type: application/json”

I’ll receive

{"detail":"Invalid accept header or version date.","error":406,"errorCode":"INVALID_VERSION_DATE","parameters":[],"reason":"Not Acceptable"}* Connection #0 to host cloud.mongodb.com left intact

Please do share where did I go wrong?

Updated.

I recently tried this but comes back with a different error

curl -v -X POST https://cloud.mongodb.com/api/atlas/v2/groups/{groupID}/customDBRoles/roles --user "{username}:{password}" --digest --header "Accept: application/vnd.atlas.2023-02-01+json" --header "Content-Type: application/vnd.atlas.2023-02-01+json" --data '{"actions":[{"action":"REMOVE","resources":[{"collection":"","db":"ambience"}]}],"inheritedRoles":[],"roleName":"customremoveRole"}'

The error is

{"detail":"Received JSON is malformed.","error":400,"errorCode":"MALFORMED_JSON","parameters":[],"reason":"Bad Request"}* Connection #0 to host cloud.mongodb.com left intact

Please advise what went wrong with the payload?

Hey @David_Aw,

Welcome to the MongoDB Community forums!

I tried the following command, and it worked for me in my testing environment. Could you please try it with the same after replacing it with your publickey , priatevkey , and GROUPID (also known as ProjectId )?

curl --user "{publickey}:{priatevkey}" --digest \
      --header "Content-Type: application/json" \
      --header "Accept: application/vnd.atlas.2023-02-01+json" \
      --include \
      --request POST "https://cloud.mongodb.com/api/atlas/v2/groups/{GROUPID}/customDBRoles/roles" \
      --data '
         {
            "actions":[
               {
                  "action":"REMOVE",
                  "resources":[
                     {
                        "collection":"",
                        "db":"example"
                     }
                  ]
               }
            ],
            "inheritedRoles":[],
            "roleName":"customremoveRole"
         }'

In case it doesn’t work, please share the error message you received after trying the above command.

Best regards,
Kushagra

Thanks for getting back to me.

curl -v --user "{username}:{password}" --digest --header "Content-Type: application/json" --header "Accept: application/vnd.atlas.2023-02-01+json" --include --request POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupID}/customDBRoles/roles" --data ' { "actions":[ { "action":"REMOVE", "resources":[ { "collection":"", "db":"ambience" } ] } ], "inheritedRoles":[], "roleName":"customremoveRole" }'

The error I am facing is

"detail":"Received JSON is malformed.","error":400,"errorCode":"MALFORMED_JSON","parameters":[],"reason":"Bad Request"}* Connection #0 to host cloud.mongodb.com left intact
curl: (3) unmatched brace in URL position 1:
{

The request body data syntax is incorrect. I believe you are missing two }.

The following worked for me (redacted credentials and group id):

curl --user "{pubkey}:{prikey}" --digest --header "Content-Type: application/json" --header "Accept: application/vnd.atlas.2023-02-01+json" --include --request POST "https://cloud.mongodb.com/api/atlas/v2/groups/{groupId}/customDBRoles/roles" --data '{
   "actions":[
      {
         "action":"REMOVE",
         "resources":[
            {
               "collection":"",
               "db":"ambience"
            }
         ]
      }
   ],
   "inheritedRoles":[
      
   ],
   "roleName":"customremoveRole"
}'
1 Like