From the course: Transitioning from Java to Kotlin

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Adding new products

Adding new products

- [Instructor] We can now pull items from a SQL database. How would we allow for the adding of new products to our applications database? Let's explore how to create a post request that can leverage content negotiation to parse incoming request bodies. We want to support adding new items to our database through an additional endpoint. So we're going to add a post endpoint in our routing.kt file. Like with get requests there is a post method for configuring post requests. We'll call that method and we'll use a path of /add-product and once again, add our lambda so we can configure how to respond to these requests. Now we will expect a post body for this endpoint that includes an instance of the product that should be added. We can access a post body object using the call for that given request. So in this case, I'll create a variable to hold the parsed body and we can call, call.receive and pass in the type of…

Contents