Skip to content
Ameen Abozaid
JNR-022

API clients

  • 5 Lessons
  • 1.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Postman
  • curl
  • API collections
  • environment variables

Learn first

1.5 h of practice (estimate)

Learning outcomes

  1. Organize Postman collections with environments and variables.
  2. Chain requests (capture an ID or token from one response for the next).
  3. Write curl commands for any endpoint, including headers and JSON bodies.
  4. Call an external HTTP API from Spring with RestClient.

Lessons

  1. 01Postman collections and environments
  2. 02Scripts and chained requests
  3. 03curl fluency
  4. 04Calling other APIs from Spring
  5. 05Demo: a chained Postman collection with environments

Lab and exercises

Lab

JNR-022-LAB1 · Shop collection

Outcomes: 1–4 · Time: ~1.5 h Create a shared Postman collection for Shop API with local and deployed environments and chained requests (create, read, update, delete). In the application, fetch exchange rates from an external API through RestClient, with connect and read timeouts configured.

Acceptance: the collection runs end-to-end; timeout configured.

Exercises (assessment: lab check)

IDOutcomeTask
JNR-022-EX011Create local and deployed environments with baseUrl and use {{baseUrl}} in every request.
JNR-022-EX022Chain: create a product, store its ID, then fetch it with the stored ID.
JNR-022-EX032Add tests to the chain that check status codes and that the returned id is present.
JNR-022-EX043Write curl commands for list, create (JSON body), update, and delete on products.
JNR-022-EX054Call a public JSON API with RestClient and map the response to a record.
JNR-022-EX064Configure timeouts and show what happens when the remote is slow or unreachable.
Show solutions
  • EX01 Environment variables baseUrl; requests use {{baseUrl}}/products; no host strings remain.
  • EX02 pm.environment.set("productId", pm.response.json().id), then GET {{baseUrl}}/products/{{productId}}.
  • EX03 pm.test("201", () => pm.response.to.have.status(201)); and check json.id.
  • EX04 e.g. curl -i -X PUT localhost:8080/products/1 -H 'Content-Type: application/json' -d '{...}'; curl -i -X DELETE localhost:8080/products/1.
  • EX05 client.get().uri(...).retrieve().body(MyRecord.class).
  • EX06 Build the client with request factory settings for connect and read timeouts (verify the exact API for your Spring version); catch the exception and return a clear error or fallback.

Outcome 1 is also checked by quiz Q01 and the lab's two environments.

Quiz

Outcomes: O1 collections and environments · O2 chaining · O3 curl · O4 RestClient.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1A Postman environment holds:O1
  2. 2Why not hard-code hosts and tokens in a shared collection?O1
  3. 3How does request 2 use an ID from request 1?O2
  4. 4What do Postman tests add?O2
  5. 5curl -i does what?O3
  6. 6To send JSON with curl you set:O3
  7. 7Which sets the method?O3
  8. 8RestClient is used to:O4
  9. 9A client without timeouts risks:O4
  10. 10After .retrieve(), .body(Rates.class) does:O4

Practical part

Six exercises JNR-022-EX01 to EX06 plus the collection lab. Pass: at least five exercises and a lab where the collection runs end to end and the RestClient has timeouts.

Common mistakes

Hard-coded hosts and tokens in collections; HTTP clients without timeouts.

Assessment

Lab check.

Career relevance

API testing (JNR-046) and resilience (MID-033).

Type to search levels, phases, modules and lessons.