Skip to content
CodeKruPro

Automate a token flow: log in, then create an order

Automation taskMediumJava onlyAsked in: Capgemini

"Automate a flow where you log in, take the token and use it in the next call" tests extraction, headers and chaining in one task. This lesson is that exact flow.

API Workflow - Authentication and Order

API Workflow - Authentication and Order

Real-world APIs require multi-step workflows, where one response feeds the next request. This assignment chains:

  1. Login: POST to /auth/login → read the logged-in user and the JWT token
  2. Create Order: POST to /orders with userId and productId, sending the token in an Authorization: Bearer <token> header → get the order confirmation

The order response includes expanded user and product data, for example user.name and product.name.

On this practice API, /orders does not check the token, so an order without it still succeeds here. Real order APIs answer 401 without one, so send it the way a real client would.

Task:

  1. Log in as admin / secret123 and print the user field after the label Logged in as: (never print the token itself; it changes every second)
  2. Create the order from the starter code with the Bearer header, and validate status 201
  3. Print the order ID, user name, product name, total and status as shown below

Expected output:

Logged in as: admin
Order ID: ORD-001
User: John Doe
Product: Wireless Mouse
Total: 29.99
Status: confirmed