It's all about the answers!

Ask a question

How to add a user/roles to DNG programmatically through API? Using Python requests


Jacob Harland (113) | asked Jun 16 '22, 11:47 a.m.

 I've been running into issues as to how to add users through the API. I've been looking at this website: https://jazz.net/wiki/bin/view/Main/DraftTeamProcessRestApi#Members_collection and specifically the section "Members collection: POST". I've copied the data in the request body example and edited it to fit my services. I then added it to an xml file and as a string. I've tried posting both of them but I keep getting a 400 "If the request body is invalid" error. Could anyone point me in the right direction for this? 

s = requests.Session()
eaders3 = {
'Accept': 'application/rdf+xml',
'OSLC-core-version': '2.0',
'Content-Type': 'application/x-www-form-urlencoded',
#'Content-Type': 'application/xml;charset=UTF-8',
}

with open('pa-members-to-add.xml', 'rb') as f:
data = f.read().replace(b'\n', b'')

print(s.post(url3, headers=headers3, data=data))

I've tried changing data to data = xml as well. xml is a String """ with the edited request body example. 


Comments
Ian Barnard commented Jun 16 '22, 11:57 a.m. | edited Jun 16 '22, 12:00 p.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

What version+ifix are you using?


You're trying to add a member to a project, with a role?

IME It's always best to get an API working using a REST client in a logged-in browser and only once that works move to coding reproducing exactly what you manually posted.

Please edit an example of a rejected body, precisely as POSTed by your code.

Also please show the precise URL you're posting to,  exactly as used by your s.post() call. - NOTE we do need the exact URL with all its parameters/encoding, because it's the combination of the URL, the headers and the body that's being rejected.

Unless you want to completely replace the project members you should first GET the existing members, then add an entry for the member you want to add.

Have you tried GETting the current members and checking the XML is as you expect? It's possible the current XML may have different tags/hierarchy or something - GETting the current members will mean you can check this.


Ian Barnard commented Jun 16 '22, 12:05 p.m. | edited Jun 16 '22, 12:08 p.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

 Also, you should be using header:


'Content-Type': 'application/xml;charset=UTF-8'
    
as shown on the page you link to - you are sending RDF XML


Jacob Harland commented Jun 16 '22, 12:27 p.m.

Yes I'm trying to add users and roles to an already created project. 

I've managed to log in manually as well as through code.  

The exact error I'm getting is "Request could not be parsed as XML."

The url is "https://website.name.com/rm/process/project-areas/_S-01wKEAEeujI9hRpLoqWg/members"

I also have used a GET to see the current xml and it matches with what I have input. I just did not show it in the code. 

I changed the Content type to what you said. I previously had it but I kept getting a 404 error. Now that I changed the print statement to output the error code though it's saying "User 'testuser1' was not found". So at this point it seems like I need to create a test user and then add them to the project I think. Do you know if the DNG API is able to create users or only add them to project? 


Ralph Schoon commented Jun 20 '22, 2:27 a.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

Yes, it is absolutely necessary that the user already exists in the server. You can use repotools commands e.g. -importUsers or -createUser or synchronize with LDAP. I am a ware of Java API that can be used, there is likely some WEB API to manage users, but I have not looked at that. The rest API explained in https://jazz.net/wiki/bin/view/Main/DraftTeamProcessRestApi  seems to be only to manage on a project area level. You would have to make sure the users exist, before you add them.

One answer



permanent link
Ian Barnard (2.1k613) | answered Jun 16 '22, 3:52 p.m.
FORUM ADMINISTRATOR / FORUM MODERATOR / JAZZ DEVELOPER

 The user must already exist. No AFAIK you can't create a user using this API - for Liberty user registry you can use repotools-jts -importUsers , see https://www.ibm.com/docs/en/elm/7.0.2?topic=reference-importusers or for LDAP the users must exist in LDAP and then I guess (haven't tried it) =importUsers can assign licenses.

Your answer


Register or to post your answer.


Dashboards and work items are no longer publicly available, so some links may be invalid. We now provide similar information through other means. Learn more here.