Using a top-level function for a handler in Kotlin
To create a handler in Kotlin, you can set a top-level function
Here is an example of a handler that accepts and returns a number:
fun handle(s: Int): Int = s
Warning
Use only one parameter to provide in top-level functions.
You can test the function from the above example in the following ways:
-
In the management console
, in the Testing tab on the function page. -
Using the following HTTPS request with the ?integration=raw parameter:
curl \ --header "Authorization: Bearer <IAM_token>" \ --data "<number>" \ "https://functions.yandexcloud.net/<function_ID>?integration=raw"
In the
--data
parameter, specify the number to be returned by the function.
Examples
Processing an HTTP request
The script below processes an incoming HTTP request and outputs the results: HTTP response code and response body.
Handler.kt
file:
data class Request(
val httpMethod: String?,
val headers: Map<String, String> = mapOf(),
val body: String = ""
)
data class Response(
val statusCode: Int,
val body: String
)
fun handle(request: Request): Response {
return Response(200, "Hello World!")
}
The result format depends on whether the user provided the ?integration=raw
parameter in the request:
-
If
?integration=raw
was provided:{ "statusCode": 200, "body": "Hello World!" }
-
If
?integration=raw
was not provided:"Hello World!"
The
200
code will be delivered not as part of the response body, as in the case with the?integration=raw
parameter, but as an HTTP response code.
HTTP request structure output
The script below processes an incoming HTTP request and outputs its structure and HTTP response code.
Handler.kt
file:
data class Response(
val statusCode: Int,
val body: String
)
fun handle(request: String): Response {
return Response(200, request)
}