Core Java · Chapter 43 of 48
The HTTP client
By the end of this chapter you can call a web service from Java: send a GET or a POST, read the status and the body, and give up in time when the server is slow. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Beyond the basics
- 5 tasks
- Java
- In the all labs plan
The first 2 chapters of Core Java are free. This one comes with the all labs plan.
Why you need this
Tapri HQ in Bengaluru now runs a small web service with the menu, today's prices and a feed where every counter reports its sales. Meera wants the counters to stop using a printed price list: "If HQ changes the price of bun maska, every counter should know by the next train." Ravi adds: "The station Wi-Fi at CSMT is awful. If HQ is slow, the counter must not freeze with a queue in front of it."
So far your programs only read files and a database on the same machine: nothing you know can ask HQ's computer for a price.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
MenuClient.java: fetch the menu and turn its JSON into a map
In MenuClient.java, make menu() send a GET to baseUrl plus /menu and return HQ's menu as a map from item name to price, in the same order as HQ sent them. HQ answers 200 with one flat JSON object, names in double quotes and prices as whole numbers. Any status other than 200: throw an IllegalStateException.
For example, the body {"Masala chai":40,"Cutting chai":25,"Bun maska":50} gives a map with Masala chai=40, Cutting chai=25 and Bun maska=50, in that order.
- Task 2
MenuClient.java: fetch one price and handle the status codes
In MenuClient.java, make price(code) send a GET to baseUrl plus /prices/ and the code. HQ answers 200 with the price as plain text, which may end with a line break; 404 when there is no such item. Return an Optional with the price for 200, an empty Optional for 404, and for any other status throw an IllegalStateException whose message contains the status number.
For example, price("masala") returns Optional[40], price("cutting") returns Optional[25] (the body is 25 and a line break), price("coffee") returns an empty Optional (404), and price("broken") throws an IllegalStateException whose message contains 500.
- Task 3
MenuClient.java: build the JSON text for a sale
In MenuClient.java, make saleJson(station, item, qty) return the JSON text for one sale, with no spaces: {"station":"...","item":"...","qty":...}, keys in that order, the two strings in double quotes and qty as a bare number. Inside the strings, write each backslash as two backslashes and each double quote as a backslash and a quote. An apostrophe stays as it is.
For example, saleJson("SBC", "Masala chai", 2) returns {"station":"SBC","item":"Masala chai","qty":2}; the item Nani's "special" chai becomes "Nani's \"special\" chai", and back\slash becomes "back\\slash".
- Task 4
MenuClient.java: send a sale to HQ with a POST and read back its id
In MenuClient.java, make postSale(station, item, qty) send a POST to baseUrl plus /sales whose body is exactly saleJson(station, item, qty), with the header Content-Type set to application/json. HQ answers 201 with a body like {"id":17}: return that id as an int. 400 means HQ refused the sale: throw an IllegalArgumentException whose message is exactly HQ's body. Any other status: throw an IllegalStateException.
For example, postSale("SBC", "Masala chai", 2) returns 17; postSale("SBC", "Masala chai", 0) throws an IllegalArgumentException with the message qty must be positive.
- Task 5 · interview question
Interview: MenuClient.java, stop a slow server from freezing the counter
In MenuClient.java, give every request you build a request timeout of the client's timeout field, so a server that takes too long to answer makes send throw an HttpTimeoutException instead of waiting. Let that exception leave the method: do not catch it.
For example, with a timeout of 300 ms, price("slow") on a server that takes seconds to answer throws an HttpTimeoutException after about 300 ms. The check calls price from task 2, so do that task first.
What you will learn
- HttpClient and HttpRequest: building a GET or a POST and sending it with java.net.http.
- Status codes: why a 404 or a 500 is an answer you must read, not an exception.
- JSON as text: writing a small JSON body by hand, and what needs escaping.
- Timeouts: the two kinds, and what happens without them.
More in Beyond the basics
Build The HTTP client yourself
Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Core Java are free. This one comes with the all labs plan.