It's all about the answers!

Ask a question

How to login via the REST API?


Mike Shkolnik (9657148143) | asked Jul 29 '13, 8:42 p.m.
I can pass HTTP commands via the REST API without issue if I am already logged in via another browser window. However if I am not, I get the prompt for username and password. How do I pass the username and password as part of the HTTP command via the REST API? For example, what would you add to the following to also login?

https://SERVER:PORT/jts/rpt/repository/foundation?fields=foundation/contributor/(userId|name|archived)&size=999

I tried &usedId=xxxxx&password=xxxx but that was ignored - didn't even get an error.

Accepted answer


permanent link
sam detweiler (12.4k6173200) | answered Jul 29 '13, 9:26 p.m.
logon is a two step process, it cannot be embedded in another call.
 here is a little batch file using curl that will logon and get the list of projects on a server
set the user, password and host variables

set COOKIES=.\cookies.txt

set USER=
set PASSWORD=
set HOST=https://localhost:9743

curl -k -c %cookies% "%host%/jts/authenticated/identity"

curl -k -L -b %COOKIES% -c %COOKIES% -d j_username=%USER% -d j_password=%PASSWORD% "%host%/jts/authenticated/j_security_check"

curl -k -L -b %COOKIES% "%host%/ccm/oslc/workitems/catalog
Ralph Schoon selected this answer as the correct answer

Comments
Mike Shkolnik commented Jul 30 '13, 1:00 p.m.

Turns out curl is not necessary. I sent the following http commands without needing curl:

https://SERVER:PORT/jts/authenticated/identity
https://SERVER:PORT/jts/authenticated/j_security_check?j_username=USERID&j_password=PASSWORD

The second command returned the roles of the user, thus enabling confirmation of successful login.

Couldn't have done it without your help, though. Thanks.


sam detweiler commented Jul 30 '13, 1:12 p.m. | edited Jul 30 '13, 1:17 p.m.

correct, you don't NEED curl. bit using it demonstrates the need and solution for the session cookie returned from the 1st call.
unless you managed this is your code, the second should have failed.
if you are running with the default, Form registration.


Mike Shkolnik commented Jul 30 '13, 6:44 p.m.

I am hoping that cookie management is automatic and not something I'll have to worry about, at least that has been the case in the past. I'll find out when I try to create code with these calls this weekend.


sam detweiler commented Jul 30 '13, 8:26 p.m.

I've never seen ANY code do automatic cookie mgmt..

One other answer



permanent link
Dibyandu Roy (111) | answered Jun 09 '16, 6:40 a.m.
Hi Mike,
This was working on 5.0.2 and in 6.0.1 its not working.
Would you please provide how to login to 6.0.1 via restapi.

Regards,
Dibyandu Roy

Comments
sam detweiler commented Jun 09 '16, 7:58 a.m.

my script above works the same in 6.0.1

Your answer


Register or to post your answer.