How to pass access token in a request from Jasper report server to JSON data API after sending a login request

0

I am trying to create reports where data is coming from JSON API.

I am having a JSON API server in the following format:

  1. A login request is sent to the server including username and password
  2. After authentication, an access token is sent back as a response.
  3. This token is then sent with data requests in the headers.
  4. After the access token verification, API responds with the requested JSON data.
    here is a diagram explaining this concept.

 

My Question   

Is there any way I could achieve this without breaking the sequence by sending the requests from the JasperReport server. I am using version 7 and is in a docker container. I am not much familiar with this kind of stuff from jasperserver. Any suggestions would be really appreciated. Thanks a lot everyone

jamshaidiqbal120's picture
Joined: Nov 25 2019 - 11:04am
Last seen: 1 week 4 days ago

3 Answers:

1

Hi jamshaidiqbal120

I had to figure this out like a month ago and this took me days to set up correctly....
You need 2 data adapters. The first one is the POST request to get the token, the second one is the GET request where you have to put the token as http header.
So once you've set up this correctly, you wil be able to use the token in the GET request. I assume this token is only valid for about 15 minutes.

The POST request:
Content-Type is very important to add because certain API applications return no result if this is missing.



If this works well and you can show the access token on your report we can proceed with the GET Request.
The URL Parameters speaks for themselfs. The http headers here below:


I don't know the kind of token you have to use but mine was a bearer token.
You can also use Postman to simulate this. Hopefully you will get a result. (POST and GET)

Now comes the tricky part...
The problem with Bearer token is that you only get a token without Bearer in front of it. So you must add "Bearer "+ token as parameter => Note the whitespace in "Bearer "
So you create a parameter. Give it a name: token and the value is the token you get from the POST Request.

To use the second data adapter to make the GET call, I've used a list element. This list element is the datasource of the GET request.
I had tried a sub-report but no luck.

I can provide you a working example but the report is on another laptop I don't have with me right now.

Let me know if you've got it to work.

Greets A3an

A3an's picture
65
Joined: Apr 10 2016 - 3:13am
Last seen: 6 days 26 min ago
0

@A3an It would be really great help if you upload a working example as you get to your laptop. thanks a lot.

 

jamshaidiqbal120's picture
Joined: Nov 25 2019 - 11:04am
Last seen: 1 week 4 days ago
1

Hi jamshaidiqbal120

Please see attached pdf.

Greets A3an

Attachments: 
A3an's picture
65
Joined: Apr 10 2016 - 3:13am
Last seen: 6 days 26 min ago
Feedback