Skip to content
Server Plugin

XHttpMethodOverride

必要相依性:io.ktor:ktor-server-method-override

程式碼範例: json-kotlinx-method-override

Native 伺服器Ktor 支援 Kotlin/Native,可讓您在無需額外執行階段或虛擬機的情況下執行伺服器。支援:✅

XHttpMethodOverride 外掛程式提供了在 X-HTTP-Method-Override 標頭中建立 HTTP 動詞通道的功能。 如果您的伺服器 API 處理多種 HTTP 動詞(GET、PUT、POST、DELETE 等),但用戶端由於特定限制只能使用有限的動詞集合(例如 GET 和 POST),此功能便非常實用。 例如,若用戶端發送的請求中將 X-Http-Method-Override 標頭設定為 DELETE,Ktor 就會使用 delete 路由處理常式 來處理此請求。

新增相依性 ​

若要使用 XHttpMethodOverride,請將 ktor-server-method-override 構件新增至您的建置指令碼中:

kotlin
implementation(ktorLibs.server.methodOverride)
上述程式碼片段使用 Ktor 版本目錄 來指定 Ktor 相依性。
groovy
implementation ktorLibs.server.methodOverride
上述程式碼片段使用 Ktor 版本目錄 來指定 Ktor 相依性。
xml
<dependency>
    <groupId>io.ktor</groupId>
    <artifactId>ktor-server-method-override-jvm</artifactId>
    <version>${ktor_version}</version>
</dependency>

安裝 XHttpMethodOverride ​

若要在您的應用程式中安裝 XHttpMethodOverride 外掛程式, 請將其傳遞給指定模組模組可讓您透過對路由進行分組來建構應用程式。中的 install 函式。 以下範例展示了如何安裝 XHttpMethodOverride:

  • 在 embeddedServer() 函式呼叫中。
  • 在 Application 類別上明確定義的 module() 擴充函式中。
kotlin
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.server.application.*
import io.ktor.server.plugins.methodoverride.*

fun main() {
    embeddedServer(Netty, port = 8080) {
        install(XHttpMethodOverride)
        // ...
    }.start(wait = true)
}
kotlin
import io.ktor.server.application.*
import io.ktor.server.plugins.methodoverride.*
// ...
fun Application.module() {
    install(XHttpMethodOverride)
    // ...
}

設定 XHttpMethodOverride ​

預設情況下,XHttpMethodOverride 會檢查 X-Http-Method-Override 標頭,以決定應該由哪個路由處理該請求。 您可以使用 headerName 屬性自訂標頭名稱。

範例 ​

下方的 HTTP 請求使用 POST 動詞,並將 X-Http-Method-Override 標頭設定為 DELETE:

http
POST http://0.0.0.0:8080/customer/3
X-Http-Method-Override: DELETE

若要使用 delete 路由處理常式 處理此類請求,您需要安裝 XHttpMethodOverride:

kotlin
package com.example

import io.ktor.http.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.methodoverride.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.*
import kotlinx.serialization.json.*
import io.ktor.server.util.getValue

@Serializable
data class Customer(val id: Int, val firstName: String, val lastName: String)

fun Application.main() {
    val customerStorage = mutableListOf<Customer>()
    customerStorage.addAll(
        arrayOf(
            Customer(1, "Jane", "Smith"),
            Customer(2, "John", "Smith"),
            Customer(3, "Jet", "Brains")
        )
    )

    install(XHttpMethodOverride)
    install(ContentNegotiation) {
        json(Json)
    }
    routing {
        get("/customer/{id}") {
            val id: Int by call.parameters
            val customer: Customer = customerStorage.find { it.id == id }!!
            call.respond(customer)
        }

        delete("/customer/{id}") {
            val id: Int by call.parameters
            customerStorage.removeIf { it.id == id }
            call.respondText("Customer is removed", status = HttpStatusCode.NoContent)
        }
    }
}

您可以在此處找到完整的範例:json-kotlinx-method-override。