HTTP Get Request

As stated earlier, we will simplify the SIS API by providing a single endpoint SERVER_URL/api/courses. When a client hits this endpoint (visits the URL), it will get a list of the current courses offered by the Computer Science (CS) Department. (For brevity, we add a few of those courses in this tutorial. You can get a list of all CS courses from here.)

Update the ApiServer.java as follows:1

import static spark.Spark.*;

public class ApiServer {
  public static void main(String[] args) {
-   get("/", (req, res) -> "Welcome to CourseSearch API!");
+   get("/api/courses", (req, res) -> "List of current CS courses!");
  }
}

Run the server (stop it and run it again, if it was already running). Then, open your browser and visit http://localhost:4567/api/courses to see the message!

You can also open http://localhost:4567/api/courses in Postman:

Notice in Postman, you are making a HTTP Get request and Spark function that allows you to handle that request is also called get. The get function takes in two parameters:

  • A path (/api/courses)
  • A callback ((request, response) -> "List of current CS courses!")

The path is the part of the URL address that proceeds http://localhost:4567. A callback is a function that is called when a client sends a request (visits the URL) that matches the get function's path.


1

The notation used in displaying the code is based on Git Diff format; the line that is preceded with - and highlighted in red is replaced with the line preceded with + and highlighted in green. If you copy the code , you should not copy the +/- signs!