How to create a JWT token and use it

  • Library : Rest Assured
  • Framwork : BDD, Cucumber, Page Object Model
  • Language : JAVA
  • IDE: Eclipse
  • API : Pocketbase

Steps :

  1. Create a collection in Pocketbase – Books

Create some fields like these

{

   “id”:”test123″,

    “bookname”: “Algorithms”,

    “price”:100,

    “sellername”:”local”

    “sold”: false

}

2. Now go to edit collection>API rules>>>Here you can edit the behaviour of the api.

Now, I want this api to create a record, only when they are authorised user. 

So I set this in the create rule>> @request.auth.id != “”

Save changes.

3. Steps for Pocketbase:

So in pocketbase I have setup a users collection. Where I’ll see Auth with password.

Steps for rest assured

I need to use the JWT token to create any record in the books collection.

  1. Make a post call to(api/collections/users/auth-with-password) and get the token
  2. Use> jsonPath().getString(“token”) > to get the value of the token field
  3. Now make a post call to > api/collections/books/records
  4. In headers give the token value >> header(“Authorization”,”Bearer “+ jwttokenValue)

Now go to edit collection>API rules>>>Here you can edit the behaviour of the api.

Now, I want this api to create a record, only when they are authorised user. 

So I set this in the create rule>> @request.auth.id != “”

Save changes.

Steps for pocketable:

So in pocketbase I have setup a users collection. Where I’ll see Auth with password.

Steps for rest assured

I need to use the JWT token to create any record in the books collection.

  1. Make a post call to(api/collections/users/auth-with-password) and get the token
  2. Use> jsonPath().getString(“token”) > to get the value of the token field
  3. Now make a post call to > api/collections/books/records

• 4. In headers give the token value >> header(“Authorization”,”Bearer “+ jwttokenValue)

Leave a Reply

Your email address will not be published. Required fields are marked *