Endpoint: HTTP Post¶
We did not have time to demo this in class
We want to allow a client to "post" a course, that is, to create a course and add it to our database using our API.
We will use the same endpoint /courses
but this time, we write code to handle a post request.
1 2 3 4 5 6 7 8 9 10 11 12 | app.post("/courses", ctx -> { // Typically, the user provides the course as a JSON object. // Maps a JSON body to a Java class using JavalinJson: Course course = ctx.bodyAsClass(Course.class); // Next, add the course to our databse using our DAO: courseDao.add(course); // Conventionally, return the added course to client: ctx.json(course); ctx.contentType("application/json"); // Also, return 201 status code to say operation succeeded. ctx.status(201); }); |
Use Postman to add a course: