Created
March 11, 2025 18:14
-
-
Save antonarhipov/ec7870c4fcb0eda26c3b086c047872a7 to your computer and use it in GitHub Desktop.
test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells" : [ { | |
"metadata" : { }, | |
"cell_type" : "markdown", | |
"source" : [ "Let me help you create code to connect to a local server using Ktor client and process JSON response inLet me help you create code to connect to a local server using Ktor client and process JSON response in Kotlin Notebook.\n", "\n" ], | |
"id" : "bcf05f2f2ca68898" | |
}, { | |
"metadata" : { }, | |
"cell_type" : "markdown", | |
"source" : "First, we need to add required dependencies for Ktor client", | |
"id" : "5e796c0edebc0963" | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-03-11T18:14:28.850766Z", | |
"start_time" : "2025-03-11T18:14:28.536512Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : "%use ktor-client", | |
"id" : "a1933ec878fb46e", | |
"outputs" : [ ], | |
"execution_count" : 17 | |
}, { | |
"metadata" : { }, | |
"cell_type" : "markdown", | |
"source" : "Now let's create a simple Ktor client and make a GET request to localhost:8080", | |
"id" : "4a5f01d7b10538b9" | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-03-11T18:14:29.103598Z", | |
"start_time" : "2025-03-11T18:14:28.856656Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "import io.ktor.client.*\n", "import io.ktor.client.engine.cio.*\n", "import io.ktor.client.request.*\n", "import io.ktor.client.statement.*\n", "import kotlinx.coroutines.runBlocking\n", "import io.ktor.client.plugins.contentnegotiation.*\n", "import io.ktor.serialization.kotlinx.json.*\n", "\n", "val client = HttpClient(CIO) {\n", " install(ContentNegotiation) {\n", " json()\n", " }\n", "}\n", "\n", "runBlocking {\n", " try {\n", " val response = client.get(\"http://localhost:8000/test.json\")\n", " println(\"Status: ${response.status}\")\n", " println(\"Content: ${response.bodyAsText()}\")\n", " } catch (e: Exception) {\n", " println(\"Error: ${e.message}\")\n", " } finally {\n", " client.close()\n", " }\n", "}" ], | |
"id" : "e6d3f67fbe3c92c2", | |
"outputs" : [ { | |
"name" : "stdout", | |
"output_type" : "stream", | |
"text" : [ "Status: 200 OK\n", "Content: {\n", " \"fruit\": \"Apple\",\n", " \"size\": \"Large\",\n", " \"color\": \"Red\"\n", "}\n", "\n" ] | |
} ], | |
"execution_count" : 18 | |
}, { | |
"metadata" : { }, | |
"cell_type" : "markdown", | |
"source" : "If you want to parse the JSON response into a data class, first create a data class matching your JSON structure (modify according to your actual JSON structure):", | |
"id" : "7b279a93739e3c7e" | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-03-11T18:14:29.360538Z", | |
"start_time" : "2025-03-11T18:14:29.108716Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "import kotlinx.serialization.Serializable\n", "\n", "@Serializable\n", "data class ResponseData(\n", " val fruit: String,\n", " val color: String,\n", " val size: String\n", ")" ], | |
"id" : "1f852c0aeb9172db", | |
"outputs" : [ ], | |
"execution_count" : 19 | |
}, { | |
"metadata" : { | |
"ExecuteTime" : { | |
"end_time" : "2025-03-11T18:14:29.649771Z", | |
"start_time" : "2025-03-11T18:14:29.369425Z" | |
} | |
}, | |
"cell_type" : "code", | |
"source" : [ "import io.ktor.client.call.*\n", "\n", "val client = HttpClient(CIO) {\n", " install(ContentNegotiation) {\n", " json()\n", " }\n", "}\n", "\n", "runBlocking {\n", " try {\n", " val response: ResponseData = client.get(\"http://localhost:8000/test.json\").body()\n", " println(\"Parsed response: $response\")\n", " } catch (e: Exception) {\n", " println(\"Error: ${e.message}\")\n", " } finally {\n", " client.close()\n", " }\n", "}" ], | |
"id" : "550f1bbc44376d8b", | |
"outputs" : [ { | |
"name" : "stdout", | |
"output_type" : "stream", | |
"text" : [ "Parsed response: ResponseData(fruit=Apple, color=Red, size=Large)\n" ] | |
} ], | |
"execution_count" : 20 | |
}, { | |
"metadata" : { }, | |
"cell_type" : "markdown", | |
"source" : "Note: Make sure your local server is running on port 8080 before executing these cells. Adjust the URL and data class structure according to your specific API endpoint and response format.\n", | |
"id" : "dbfbcbb77c6af7f3" | |
} ], | |
"metadata" : { | |
"kernelspec" : { | |
"display_name" : "Kotlin", | |
"language" : "kotlin", | |
"name" : "kotlin" | |
}, | |
"language_info" : { | |
"name" : "kotlin", | |
"version" : "1.9.23", | |
"mimetype" : "text/x-kotlin", | |
"file_extension" : ".kt", | |
"pygments_lexer" : "kotlin", | |
"codemirror_mode" : "text/x-kotlin", | |
"nbconvert_exporter" : "" | |
}, | |
"ktnbPluginMetadata" : { | |
"projectLibraries" : false | |
} | |
}, | |
"nbformat" : 4, | |
"nbformat_minor" : 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment