| Item | Detail |
|---|---|
| User | +++@lab.VirtualMachine(desktop1).Username+++ |
| Password | +++@lab.VirtualMachine(desktop1).Password+++ |
When: This is the Workshop Skills Application. Complete it after Workshop 1 has ended, in the two hours that follow.
You will implement an API client in Java that sends HTTP requests (GET, POST, PUT, DELETE) to a REST API, and analyse responses (status codes, headers, JSON body). Use the provided starter project and the mock API JSONPlaceholder (https://jsonplaceholder.typicode.com) to test your implementation.
Learning objectives:
- Implement API requests using HTTP methods (GET, POST, PUT, DELETE) with appropriate headers and request bodies
- Analyse API responses, including status codes, headers, and data formats (JSON) to extract relevant information
- Handle error responses appropriately
In ApiClient.java, implement the get(String path) method.
Requirements:
- Build an HTTP GET request to
baseUrl + path(e.g.https://jsonplaceholder.typicode.com/posts/1). - Set the header
Accept: application/json. - Send the request using
HttpClientand obtain the response. - If the status code is in the 2xx range, return the response body as a string.
- For non-2xx responses, handle appropriately (e.g. throw an exception with status code and body, or return error details).
Verification: Running the application and calling client.get("posts/1") should return the JSON for post with id 1.
In ApiClient.java, implement the post(String path, String jsonBody) method.
Requirements:
- Build an HTTP POST request to
baseUrl + pathwith a request body. - Set headers:
Content-Type: application/jsonandAccept: application/json. - Send the request with the provided
jsonBodyand return the response body as a string. - Expect a 201 Created (or 200) for successful creation; handle other status codes appropriately.
Verification: Calling client.post("posts", "{\"title\":\"Lab\",\"body\":\"Test\",\"userId\":1}") should return the created resource (or confirmation) from the API.
In ApiClient.java, implement put(String path, String jsonBody) and delete(String path).
Requirements for PUT:
- Build an HTTP PUT request with
Content-Type: application/jsonandAccept: application/json. - Send the body and return the response body as a string.
Requirements for DELETE:
- Build an HTTP DELETE request to the given path.
- Return the HTTP status code (e.g. 200 or 204) from the response.
Verification: PUT to posts/1 with valid JSON should return updated data; DELETE to posts/1 should return a success status code.
Requirements:
- Ensure all methods check the response status code before returning.
- Add simple error handling: for 4xx or 5xx, either throw an exception that includes the status code and response body, or return a clear error message.
- Create a short document
response_handling_notes.txtthat describes:- How you determined success (2xx) vs client error (4xx) vs server error (5xx).
- How you would extract specific fields from a JSON response (e.g. using a library or manual parsing).
- One example of a real-world API (e.g. weather, payments) and what status codes you might expect for success and failure.
- GET, POST, PUT, and DELETE are implemented with correct headers and (where applicable) request bodies.
- Response bodies are returned as strings for 2xx; non-2xx is handled with clear error behaviour.
- DELETE returns the HTTP status code.
response_handling_notes.txtis completed as described.
./mvnw spring-boot:runOr with Maven installed:
mvn spring-boot:runUncomment the lines in Lab01ApisServicesApplication.run() to exercise your ApiClient methods.
- Starter code: This repository (ApiClient.java and main application).
- Mock API: https://jsonplaceholder.typicode.com/ (GET/POST/PUT/DELETE on
/posts,/users, etc.). - Java HttpClient: https://docs.oracle.com/en/java/javase/17/docs/api/java.net.http/java/net/http/HttpClient.html
@lab.Activity()