Hello and welcome to Red Stapler. Today
We will talk about JIRA REST API with
Cookie based authentication and here is
atlassian developer tutorials which is
detailed and i think i should simplify it
a bit
The first thing we are going to talk
about is to create a new session using
JIRA REST API which actually you just need to
make a post request to this URL with a
username and password credentials in
JSON format and if it success, if the
credential you provided is correct,
you'll got a json response in this format
which is very important. The JSESSIONID you
got, you will be using it as a cookie
header in the subsequent requests so
here's just step two is to use the
session cookie in a request
basically you just need to set the
cookie header of all subsequent request
with the JSESSIONID you just got and
that's it! You are authenticated. Let's
see it in action
Here I made a simple form with a
username and password and two buttons
the first button is to login with the
cookie authentication and the second is
to retrieve the user profile which we
intended to use it after we have
authenticated. Both buttons will make a
post request to each PHP file. The
login buttons will post to jiracookie.php
to PHP and retrieve user profile button will post to
jiraprofile.php. Let's see the jiracookie.php first
first we're going to initialize the curl
request with the REST API URL and then we
are going to get the username and
password which is psoted to this page
and then encode it in JSON format and then
we set the curl POST fields with the
username and password JSON and then we
set the POST option to true since we are making
POST request, not GET. Set the RETURNTRANSFER
to true since we want to get
a full response result, not just
return code. And the most vital part is
HTTP headers we need to set the content
type to application/json. Then we
make the actual request. We execute the
curl and once it's completed. We'll decode
the JSON respond and here's the error checking
steps if there's an error. If the
credential is not correct
you'll got an error message back and we
will return it but if the credential is
correct we will just set the cookies
with the name and value returned from
the session JSON
so that's it that's our authentication
page. The next is jiraprofile.php which
basically quite the same. We just need to
initialize request to retrieve user info
set the RETURNTRANSFER and HTTP
headers to application/json. And here's
the key. You need to check if there's the
cookies
I mean JSESSIONID cookie available
if there is we just build a cookie
string with the JSESSIONID but
if not I would just use the blank string
and then you set the cookie headers
with the cookies string we just built
and then be executed curl. Then we decode
result and check the errors. If its
success we return the display name and
email address and if not return the
error message. Now let's see it in action
i'm going to try hitting the retrieve user
info without loggin first. You see you
'll be greeted with the error message. You
are not authenticated. Authentication
required to perform this operation which
is the error message return from the
JIRA server
now let's see if I hit the login button
without putting anything on the form
see login failed
let's try logging in
ok login success and then retrieve the
user profile. That's it. We got the username
and email address
displayed on screen. Since we are using
cookies authentication I don't need to
do the login anymore. I can just
retrieve whatever I want
so that's it. Thanks for watching as
always if you have any comment any
feedback any questions just let me know
in a comment section and don't forget to
like or subscribe! Bye :)