Skip to content

Latest commit

 

History

History
132 lines (89 loc) · 7.14 KB

File metadata and controls

132 lines (89 loc) · 7.14 KB
<style> .box { display: Inline-block; text-align: center; padding: 15px; background-color: #23EB9A; border-radius: 10px; align-items: Center; display: flex; justify-content: center; } </style>

Workshop 1: APIs and Services – Implement API Requests and Analyse Responses


Access and Credentials

Item Detail
User +++@lab.VirtualMachine(desktop1).Username+++
Password +++@lab.VirtualMachine(desktop1).Password+++

Challenge overview

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

Your tasks

Task 1: Implement GET request (25 points)

In ApiClient.java, implement the get(String path) method.

Requirements:

  1. Build an HTTP GET request to baseUrl + path (e.g. https://jsonplaceholder.typicode.com/posts/1).
  2. Set the header Accept: application/json.
  3. Send the request using HttpClient and obtain the response.
  4. If the status code is in the 2xx range, return the response body as a string.
  5. 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.


Task 2: Implement POST request (25 points)

In ApiClient.java, implement the post(String path, String jsonBody) method.

Requirements:

  1. Build an HTTP POST request to baseUrl + path with a request body.
  2. Set headers: Content-Type: application/json and Accept: application/json.
  3. Send the request with the provided jsonBody and return the response body as a string.
  4. 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.


Task 3: Implement PUT and DELETE requests (25 points)

In ApiClient.java, implement put(String path, String jsonBody) and delete(String path).

Requirements for PUT:

  1. Build an HTTP PUT request with Content-Type: application/json and Accept: application/json.
  2. Send the body and return the response body as a string.

Requirements for DELETE:

  1. Build an HTTP DELETE request to the given path.
  2. 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.


Task 4: Response analysis and error handling (25 points)

Requirements:

  1. Ensure all methods check the response status code before returning.
  2. 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.
  3. Create a short document response_handling_notes.txt that 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.

Success criteria

  • 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.txt is completed as described.

Running the project

./mvnw spring-boot:run

Or with Maven installed:

mvn spring-boot:run

Uncomment the lines in Lab01ApisServicesApplication.run() to exercise your ApiClient methods.


Resources

@lab.Activity()