<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: KMeshU</title>
    <description>The latest articles on DEV Community by KMeshU (@kumbharumesh).</description>
    <link>https://dev.to/kumbharumesh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F558227%2F82700b18-0c75-4868-bcb2-52cf38d17271.jpeg</url>
      <title>DEV Community: KMeshU</title>
      <link>https://dev.to/kumbharumesh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kumbharumesh"/>
    <language>en</language>
    <item>
      <title>Writing your first HTTP API using Ktor!</title>
      <dc:creator>KMeshU</dc:creator>
      <pubDate>Sat, 16 Jan 2021 16:52:03 +0000</pubDate>
      <link>https://dev.to/kumbharumesh/write-your-first-http-api-using-ktor-1dcd</link>
      <guid>https://dev.to/kumbharumesh/write-your-first-http-api-using-ktor-1dcd</guid>
      <description>&lt;h1&gt;
  
  
  Introduction
&lt;/h1&gt;

&lt;p&gt;Ktor is a Kotlin framework that can be used to create a variety of server (and client-side) applications. &lt;/p&gt;

&lt;p&gt;Without further delay, let's start writing the application. &lt;/p&gt;

&lt;p&gt;Like &lt;a href="//start.spring.io"&gt;Spingboot initializer&lt;/a&gt; which creates a Spring boot project with all the dependencies you need to begin quickly, there is a Ktor Project Generator available. &lt;/p&gt;

&lt;p&gt;Visit &lt;a href="https://start.ktor.io/#"&gt;Ktor Project Generator&lt;/a&gt;. Choose Gradle Project, Jetty server, and Ktor 1.5.0. Then updated Group, Name, and version as per your requirements. For now, let's not choose anything on the right-hand side from the Server and Client section. Click Build. It will create an archive that contains the base structure of the project where you can start writing your code in the "Application.kt" file.&lt;br&gt;&lt;br&gt;
Extract the archive. Open the project in IntelliJ Idea or any other IDE you are using for Kotlin development. Now you can directly jump to writing the code in the "Application.kt" file.&lt;/p&gt;

&lt;p&gt;Update the "Application.kt" as below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import io.ktor.application.*
import io.ktor.response.*
import io.ktor.routing.*

fun main(args: Array&amp;lt;String&amp;gt;): Unit = io.ktor.server.jetty.EngineMain.main(args)

@Suppress("unused")
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {
    routing {
        get("/") {
            call.respondText("Hello, world!")
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your first API is ready to execute now. You can run the application using ./gradlew commands or run directly from IntelliJ. (Green triangle icon is shown on the left-hand side of the main function).&lt;br&gt;
Hit &lt;a href="HTTP://localhost:8080"&gt;HTTP://localhost:8080&lt;/a&gt; from the browser. You should see the output as shown in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Z05XGRI8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9bwbp53g222plvfb69dc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Z05XGRI8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/9bwbp53g222plvfb69dc.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Deep Dive
&lt;/h1&gt;
&lt;h3&gt;
  
  
  Engine
&lt;/h3&gt;

&lt;p&gt;Inside the main function, we are specifying the server engine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fun main(args: Array&amp;lt;String&amp;gt;): Unit = io.ktor.server.jetty.EngineMain.main(args)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To run a Ktor server application, you need to create and configure a server first. EngineMain represents an engine for running a server. Ktor supports the following engines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Netty&lt;/li&gt;
&lt;li&gt;Jetty&lt;/li&gt;
&lt;li&gt;Tomcat&lt;/li&gt;
&lt;li&gt;CIO (Coroutine-based I/O)
Ktor also provides a special engine type TestEngine for testing application logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can define different server properties like various engine-specific options, host and port values, and so on in the "application.conf" file under the "resources" folder. &lt;/p&gt;

&lt;h3&gt;
  
  
  Routing
&lt;/h3&gt;

&lt;p&gt;Ktor Application consists of one or more modules. The module is nothing but a user-defined function that is receiving &lt;strong&gt;Application&lt;/strong&gt; class that is in charge of configuring the server pipeline, install features, registering routes, handling requests, etc. &lt;/p&gt;

&lt;p&gt;In the above example &lt;strong&gt;module&lt;/strong&gt; is an extension method of the class Application.&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;code&gt;fun Application.module(testing: Boolean = false)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0cqNQlkY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/imxlyudi893zj6gbac4c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0cqNQlkY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/imxlyudi893zj6gbac4c.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Routing is the one that allows incoming requests to be handled. When a request such as &lt;strong&gt;"/"&lt;/strong&gt; in our case is made, the routing mechanism in Ktor allows us to define how we want this request to be served.&lt;br&gt;
Any &lt;strong&gt;route&lt;/strong&gt; takes three parameters: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;URL pattern&lt;/li&gt;
&lt;li&gt;Verb,(GET, POST, PUT, DELETE, HEAD, OPTION, or PATCH)&lt;/li&gt;
&lt;li&gt;Handler - access to handling the request&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can define multiple routes as shown below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;routing {
    get("/") {

    }
    post("/user") {

    }
    get("/user/{id}") {

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://unsplash.com/s/photos/kotlin?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Cover Photo by Marc Reichelt&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>kotlin</category>
      <category>ktor</category>
      <category>httpapi</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
