{"id":60297,"date":"2024-02-08T14:18:45","date_gmt":"2024-02-08T08:48:45","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=60297"},"modified":"2024-02-26T17:40:42","modified_gmt":"2024-02-26T12:10:42","slug":"introduction-to-ktor-rest-api","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/introduction-to-ktor-rest-api\/","title":{"rendered":"Introduction to Ktor Rest API"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-60296\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Untitled-Design-1024x683.png\" alt=\"\" width=\"769\" height=\"513\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Untitled-Design-1024x683.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Untitled-Design-300x200.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Untitled-Design-768x512.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Untitled-Design-624x416.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Untitled-Design.png 1500w\" sizes=\"(max-width: 769px) 100vw, 769px\" \/><\/p>\n<p>For Kotlin, <strong>Ktor<\/strong> is a lightweight and adaptable web framework that may be used to create high-performing, scalable web applications. Its extensibility and simplicity of usage, which enable developers to develop and launch applications fast with a clear and simple syntax, are the main reasons for its appeal. A wide range of capabilities are also available with Ktor, including as support for HTTP clients and servers, routing, templating, and asynchronous programming. Moreover, a lot of Kotlin developers prefer Ktor because of its versatility in working with various platforms and contexts, including cloud and Android environments.<\/p>\n<p>This blog will teach you the fundamentals of <strong>Ktor<\/strong> and show you how to use it to build a basic backend application.<\/p>\n<h2 id=\"87d5\" class=\"or nu gr be nv os ot dx nz ou ov dz od ng ow ox oy nk oz pa pb no pc pd pe pf bj\" data-selectable-paragraph=\"\">What is Ktor?<\/h2>\n<p>\u25cf Developed by <strong>JetBrains<\/strong>, <strong>Ktor<\/strong> is an open-source framework for creating asynchronous servers and clients with the Kotlin programming language.<\/p>\n<p>\u25cf Because it provides a flexible and lightweight method of developing web applications with an emphasis on modularization and extensibility, Ktor has gained popularity. With Kotlin&#8217;s succinct syntax and functional programming features, developers can create web applications quickly and effortlessly. Because of its extremely scalable and performant design, it may be used to create online applications with heavy traffic. It can run on conventional servers, containerized environments, and serverless platforms like AWS Lambda, among other deployment possibilities.<\/p>\n<h2 id=\"ac80\" class=\"or nu gr be nv os ot dx nz ou ov dz od ng ow ox oy nk oz pa pb no pc pd pe pf bj\" data-selectable-paragraph=\"\">Why do we need Ktor?<\/h2>\n<p><b>\u25cf Kotlin and Coroutines:\u00a0 <\/b>Building web applications using Kotlin and Coroutines in Ktor offers a contemporary and effective approach. Building and maintaining code using Kotlin&#8217;s expressive syntax with Java interoperability is simpler, and writing asynchronous code with Coroutines is scalable and efficient.<\/p>\n<p><b>\u25cf Lightweight and Flexible: <\/b>Ktor is constructed from a collection of compact, targeted libraries that can be used separately or in combination based on the project&#8217;s requirements. It uses Kotlin&#8217;s robust features, such as DSLs (domain-specific languages) and extension functions, to offer a clear and expressive syntax that lets programmers flexibly organize their applications.<\/p>\n<p><b>\u25cf Interoperable: <\/b>As Ktor is based on Kotlin, it is quite compatible with Java. Ktor&#8217;s ease of integration with pre-existing Java code and libraries allows developers to use the framework with other Java-based frameworks like Springboot or in their current Java projects.<\/p>\n<p><b>\u25cf Migration: <\/b>Because Kotlin supports progressive migration, it makes it possible for big codebases to migrate smoothly from Java to Kotlin. Kotlin allows developers to write new code while keeping existing Java components of their system intact.<\/p>\n<p><b>\u25cf Tooling support: <\/b>Developers can create, maintain, and deploy Ktor applications with ease using their favorite tools and workflows thanks to Ktor&#8217;s robust tooling support. Specifically, the tight interaction with IntelliJ IDEA offers Ktor users a seamless and effective development experience.<\/p>\n<p><strong class=\"mx gs\">\u25cf Community support: <\/strong>The Ktor community is committed to assisting developers in becoming familiar with and proficient users of the framework. It is robust and encouraging. Apart from the official documentation, the Ktor community provides assistance via other platforms like StackOverflow, Reddit, and GitHub.<\/p>\n<h2><b>Prerequisites &amp; Tools used to develop rest API<\/b><\/h2>\n<p><span style=\"font-weight: 400;\">In order to develop rest API using ktor, we need to have basic knowledge of Kotlin&#8217;s programming language and its syntax.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">We need to use the following tools in order to develop this project<\/span><\/p>\n<ul>\n<li><b>Intellij IDE<\/b><span style=\"font-weight: 400;\">: For writing, debugging, and executing the code.<\/span><\/li>\n<\/ul>\n<ul>\n<li><b>MongoDB<\/b><span style=\"font-weight: 400;\">: Database for storing the data of the project.<\/span><\/li>\n<\/ul>\n<ul>\n<li><b>NoSQLBooster: <\/b><span style=\"font-weight: 400;\">Used to view the data stored in the MongoDB.<\/span><\/li>\n<\/ul>\n<h2 id=\"e9d5\" class=\"nt nu gr be nv nw nx ny nz oa ob oc od oe of og oh oi oj ok ol om on oo op oq bj\">Creating a Ktor application<\/h2>\n<p>Depending on the specifications, several methods exist to begin your Ktor projects. You can make a Ktor project with IntelliJ IDEA Ultimate. Use the community edition, which allows you to create a Ktor project using a web-based project generator, if you&#8217;re interested in open-source development. Additionally, you can use start.ktor.io to create a new Ktor application and use IntelliJ IDEA to provide the necessary features.<\/p>\n<p>In this section, we will create a project using the Ktor Project Generator web tool.<\/p>\n<p><b>\u25cf <\/b>Use your browser to open the <strong>https:\/\/start.ktor.io\/<\/strong> URL.<\/p>\n<p><b>\u25cf <\/b>Give the project a legitimate name, such as <strong>ktor-sample<\/strong>.<\/p>\n<p><b>\u25cf <\/b>By selecting the &#8220;<strong>adjust project settings<\/strong>&#8221; link, the user can view the project configurations as they are by default.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60280 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-02-080616.png\" alt=\"\" width=\"732\" height=\"779\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-02-080616.png 732w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-02-080616-282x300.png 282w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-02-080616-624x664.png 624w\" sizes=\"(max-width: 732px) 100vw, 732px\" \/><\/p>\n<p><span style=\"font-weight: 400;\"><b>\u25cf <\/b>In this demo, the default project configurations will be used.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><b>\u25cf <\/b>Click the Add Plugins buttons to add features such as <strong>Content Negotiation<\/strong>, <strong>kotlinx.serialization<\/strong><\/span><b>, <\/b><span style=\"font-weight: 400;\"><strong>Default Headers,<\/strong> and <strong>Routing<\/strong>.<\/span><\/p>\n<p><b>\u25cf kotlinx.serialization<\/b><span style=\"font-weight: 400;\">: <strong>ContentNegotiation<\/strong> allows the users to use content converters provided by the kotlinx.serialization library. This library supports <strong>JSON<\/strong>, <strong>CBOR<\/strong>, <strong>ProtoBuf<\/strong>, and other formats.<\/span><\/p>\n<p><b>Routing<\/b><span style=\"font-weight: 400;\">: Routing is a plugin that is installed into an application to simplify and structure the page request handling. It provides a structured way to handle incoming HTTP requests and generate corresponding responses. With Ktor routing, developers can define routes that match specific request URLs and methods and then handle these requests with custom logic.<\/span><\/p>\n<p><span style=\"font-weight: 400;\">Click on Generate Project to generate the Ktor project.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-60283 size-full alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/MixCollage-10-Feb-2024-12-48-PM-6301-e1708949375609.jpg\" alt=\"\" width=\"702\" height=\"702\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/MixCollage-10-Feb-2024-12-48-PM-6301-e1708949375609.jpg 702w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/MixCollage-10-Feb-2024-12-48-PM-6301-e1708949375609-300x300.jpg 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/MixCollage-10-Feb-2024-12-48-PM-6301-e1708949375609-150x150.jpg 150w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/MixCollage-10-Feb-2024-12-48-PM-6301-e1708949375609-624x624.jpg 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/MixCollage-10-Feb-2024-12-48-PM-6301-e1708949375609-120x120.jpg 120w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/MixCollage-10-Feb-2024-12-48-PM-6301-e1708949375609-24x24.jpg 24w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/MixCollage-10-Feb-2024-12-48-PM-6301-e1708949375609-48x48.jpg 48w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/MixCollage-10-Feb-2024-12-48-PM-6301-e1708949375609-96x96.jpg 96w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/p>\n<p><span style=\"font-weight: 400;\"><b>\u25cf <\/b>Download and extract the [project-name].zip (<strong>Eg: ktor-sample.zip<\/strong>) file.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><b>\u25cf <\/b>Open it in <strong>IntelliJ IDEA<\/strong> community edition and wait for the Gradle sync completion.<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><b>\u25cf <\/b>Click on the play icon (\u25b6\ufe0f) to run the <strong>ktor server<\/strong>.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60284 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112043.png\" alt=\"\" width=\"652\" height=\"536\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112043.png 652w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112043-300x247.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112043-624x513.png 624w\" sizes=\"(max-width: 652px) 100vw, 652px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">The Run window shows a message like \u201cResponding at <\/span><a href=\"http:\/\/127.0.0.1:8080\/\"><span style=\"font-weight: 400;\">http:\/\/127.0.0.1:8080<\/span><\/a><span style=\"font-weight: 400;\">\u201d if the server is running successfully.<\/span><\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60285 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112323-1024x181.png\" alt=\"\" width=\"625\" height=\"110\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112323-1024x181.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112323-300x53.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112323-768x136.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112323-624x110.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112323.png 1045w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p><span style=\"font-weight: 400;\">The \u201c<strong>Hello World!<\/strong>\u201d message will be shown if you hit the <\/span><a href=\"http:\/\/127.0.0.1:8080\/\"><span style=\"font-weight: 400;\">http:\/\/127.0.0.1:8080<\/span><\/a><span style=\"font-weight: 400;\"> URL on the browser.<\/span><\/p>\n<h2><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60286 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112553.png\" alt=\"\" width=\"494\" height=\"166\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112553.png 494w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112553-300x101.png 300w\" sizes=\"(max-width: 494px) 100vw, 494px\" \/><\/h2>\n<h2><b>Understanding the Project Structure<\/b><\/h2>\n<p>A Ktor application&#8217;s project structure is quite adaptable and may be changed to suit the requirements of the developer. However, a Ktor project structure will typically contain the following common elements:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"wp-image-60287 size-full alignnone\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112725.png\" alt=\"\" width=\"475\" height=\"706\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112725.png 475w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/Screenshot-2023-08-26-112725-202x300.png 202w\" sizes=\"(max-width: 475px) 100vw, 475px\" \/><\/p>\n<ol>\n<li style=\"font-weight: 400;\"><strong>src<\/strong><span style=\"font-weight: 400;\"><strong> directory:<\/strong> This directory typically contains the source code for the application. It may be further divided into subdirectories based on the modules or features of the application.<\/span><\/li>\n<li style=\"font-weight: 400;\"><strong>resources<\/strong><span style=\"font-weight: 400;\"><strong> directory:<\/strong> This directory typically contains any non-source files that are required by the application, such as configuration files, static assets, or templates.<\/span><\/li>\n<li style=\"font-weight: 400;\"><strong>test<\/strong><span style=\"font-weight: 400;\"><strong> directory:<\/strong> This directory typically contains unit and integration tests for the application.<\/span><\/li>\n<li style=\"font-weight: 400;\"><strong>build.gradle.kts or pom.xml<\/strong><strong>:<\/strong> These files contain the build configuration for the application, including dependencies, plugins, and other settings.<\/li>\n<li style=\"font-weight: 400;\"><strong>application.conf<\/strong><span style=\"font-weight: 400;\"><strong>:<\/strong> This file contains the configuration settings for the Ktor application, such as server settings, logging, and security settings.<\/span><\/li>\n<li style=\"font-weight: 400;\"><strong>Application.kt<\/strong><span style=\"font-weight: 400;\"><strong>:<\/strong> This file contains the main entry point for the Ktor application, where the server is configured and started. <\/span><\/li>\n<\/ol>\n<p><span style=\"font-weight: 400;\">Additionally, a Ktor project may include other directories or files based on the user requirements, such as a database directory, custom libraries or modules, or additional configuration files.<\/span><\/p>\n<h2><b>Create your first Ktor application<\/b><\/h2>\n<p>This part will cover creating a custom Ktor application that provides the user with user information. To continue creating the application, we will use the beginning code (above), which was produced using the Ktor generator. The IntelliJ IDEA Ultimate IDE or the ktor generator can be used to create new applications if that&#8217;s your thing.<\/p>\n<p>When the Gradle sync is finished, open the downloaded project in the IntelliJ IDEA community edition.<\/p>\n<p>By right-clicking on the com.example directory, choosing New \u2192 Package, and entering a legitimate name named models, you can create a package called com.example.models.<br \/>\nMake your own paths for adding, removing, and creating users. Make a model of the user that includes the author, title, ID, and publications. For this, choose New \u2192 Kotlin by right-clicking the models folder.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60288 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1024x839.png\" alt=\"\" width=\"625\" height=\"512\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1024x839.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-300x246.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-768x629.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1536x1259.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-2048x1678.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-624x511.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<p>In this section, we will use a <strong>Mutable list-type<\/strong>\u00a0structure for adding users and simulating the data without using any databases.<\/p>\n<ul class=\"\">\n<li id=\"9243\" class=\"mv mw gr mx b my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns pl pm pn bj\" data-selectable-paragraph=\"\"><strong class=\"mx gs\">POST(\u201c\/user\u201d) \u2014 Create a new user<\/strong><br \/>\nTo create a new user, add a new endpoint as a post request to the <b>Serialization.kt<\/b>\u00a0file. Add the following code to it.<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60289 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-2-1-866x1024.png\" alt=\"\" width=\"625\" height=\"739\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-2-1-866x1024.png 866w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-2-1-254x300.png 254w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-2-1-768x908.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-2-1-1299x1536.png 1299w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-2-1-1732x2048.png 1732w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-2-1-624x738.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<ul class=\"\">\n<li id=\"4d4f\" class=\"mv mw gr mx b my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns pl pm pn bj\" data-selectable-paragraph=\"\"><strong class=\"mx gs\">GET(\u201c\/user\u201d) \u2014 Fetch the list of users<\/strong><br \/>\nOpen the <b>Serialization.kt <\/b>file, add a new endpoint as a get request to retrieve all users. Follow the below code,<\/li>\n<\/ul>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-60290 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-3-1-804x1024.png\" alt=\"\" width=\"625\" height=\"796\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-3-1-804x1024.png 804w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-3-1-236x300.png 236w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-3-1-768x978.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-3-1-1206x1536.png 1206w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-3-1-1608x2048.png 1608w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-3-1-624x795.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<ul class=\"\">\n<li id=\"14dc\" class=\"mv mw gr mx b my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns pl pm pn bj\" data-selectable-paragraph=\"\">To run the app, select the <strong>Application.kt<\/strong> file and press the play button (\u25b6\ufe0f). Open the postman and hit the URLs (Eg: <a class=\"af po\" href=\"http:\/\/127.0.0.1:8080\/book\" target=\"_blank\" rel=\"noopener ugc nofollow\">http:\/\/127.0.0.1:8080\/users<\/a>) with a JSON request body.<\/li>\n<\/ul>\n<h2 id=\"f642\" class=\"or nu gr be nv os ot dx nz ou ov dz od ng ow ox oy nk oz pa pb no pc pd pe pf bj\" data-selectable-paragraph=\"\">Creating service and routing<\/h2>\n<p>We created a basic get\/post request in the previous section. We will build a service and route that preloads user&#8217;s data in this section.<\/p>\n<p>First, leave a remark as follows on the lines you added to the <strong>Serialization.kt<\/strong> file.<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60291 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1-998x1024.png\" alt=\"\" width=\"625\" height=\"641\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1-998x1024.png 998w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1-292x300.png 292w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1-768x788.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1-1497x1536.png 1497w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1-1996x2048.png 1996w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1-624x640.png 624w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1-24x24.png 24w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-4-1-48x48.png 48w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h2 id=\"0162\" class=\"pw-post-body-paragraph mv mw gr mx b my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gk bj\"><strong class=\"mx gs\">Create the service<\/strong><\/h2>\n<p data-selectable-paragraph=\"\">Add a new Kotlin class or file called <strong>UserService.kt.<\/strong>\u00a0 Add the code that follows.<\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60292 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-5-1-889x1024.png\" alt=\"\" width=\"625\" height=\"720\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-5-1-889x1024.png 889w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-5-1-260x300.png 260w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-5-1-768x885.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-5-1-1333x1536.png 1333w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-5-1-1777x2048.png 1777w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-5-1-624x719.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h2 id=\"a48a\" class=\"pw-post-body-paragraph mv mw gr mx b my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gk bj\"><strong class=\"mx gs\">Create the route<\/strong><\/h2>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60294 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-7-1-789x1024.png\" alt=\"\" width=\"625\" height=\"811\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-7-1-789x1024.png 789w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-7-1-231x300.png 231w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-7-1-768x997.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-7-1-1183x1536.png 1183w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-7-1-1577x2048.png 1577w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-7-1-624x810.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h2 id=\"b712\" class=\"pw-post-body-paragraph mv mw gr mx b my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gk bj\"><strong class=\"mx gs\">Add the route<\/strong><\/h2>\n<p id=\"f621\" class=\"pw-post-body-paragraph mv mw gr mx b my mz na nb nc nd ne nf ng nh ni nj nk nl nm nn no np nq nr ns gk bj\" data-selectable-paragraph=\"\">Open the <strong>Routing.kt<\/strong> file and add the following code to it.<\/p>\n<p data-selectable-paragraph=\"\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-60295 size-large\" src=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-8-1-1024x658.png\" alt=\"\" width=\"625\" height=\"402\" srcset=\"\/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-8-1-1024x658.png 1024w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-8-1-300x193.png 300w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-8-1-768x494.png 768w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-8-1-1536x988.png 1536w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-8-1-2048x1317.png 2048w, \/blog\/wp-ttn-blog\/uploads\/2024\/02\/carbon-8-1-624x401.png 624w\" sizes=\"(max-width: 625px) 100vw, 625px\" \/><\/p>\n<h3 id=\"981d\" class=\"or nu gr be nv os ot dx nz ou ov dz od ng ow ox oy nk oz pa pb no pc pd pe pf bj\">Test the app<\/h3>\n<p>We can now use the get, post, and delete routes to test the app. To start the application and test the URLs using Postman, choose the <strong>Application.kt<\/strong> file and click the play button (\u25b6\ufe0f).<\/p>\n<h2><b>Reference<\/b><\/h2>\n<p><a href=\"https:\/\/codemaker2016.medium.com\/introduction-to-ktor-6e238fa20c3#87d5\"><span style=\"font-weight: 400;\">https:\/\/codemaker2016.medium.com\/introduction-to-ktor-6e238fa20c3<\/span><\/a><\/p>\n<p><a href=\"https:\/\/ktor.io\/\"><span style=\"font-weight: 400;\">https:\/\/ktor.io\/<\/span><\/a><\/p>\n<p>Check out our other blog posts for more insights. If you still have questions, comment and join the discussion.<\/p>\n<div class=\"ap-custom-wrapper\"><\/div><!--ap-custom-wrapper-->","protected":false},"excerpt":{"rendered":"<p>For Kotlin, Ktor is a lightweight and adaptable web framework that may be used to create high-performing, scalable web applications. Its extensibility and simplicity of usage, which enable developers to develop and launch applications fast with a clear and simple syntax, are the main reasons for its appeal. A wide range of capabilities are also [&hellip;]<\/p>\n","protected":false},"author":1493,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":52},"categories":[518,1772,1994],"tags":[5642,5641],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60297"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1493"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=60297"}],"version-history":[{"count":6,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60297\/revisions"}],"predecessor-version":[{"id":60458,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/60297\/revisions\/60458"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=60297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=60297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=60297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}