I am trying to create reports where data is coming from JSON API.
I am having a JSON API server in the following format:
- A login request is sent to the server including username and password
- After authentication, an access token is sent back as a response.
- This token is then sent with data requests in the headers.
- After the access token verification, API responds with the requested JSON data.
here is a diagram explaining this concept.
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
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.
Please see attached pdf.