🍻 POST-request m/ payload mot backend lagt til + jackson (heehee)
Co-authored-by: Amalie Erdal Mansåker <amalie.erdal.mansaker@nav.no> Co-authored-by: Markus A. R. Johansen <markus.aleksander.rakil.johansen@nav.no>
This commit is contained in:
parent
a8446b6b73
commit
fdf54063c2
4 changed files with 54 additions and 0 deletions
|
@ -27,4 +27,10 @@ dependencies {
|
||||||
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
|
testImplementation("io.ktor:ktor-server-tests-jvm:$ktor_version")
|
||||||
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
|
testImplementation("org.jetbrains.kotlin:kotlin-test-junit:$kotlin_version")
|
||||||
implementation("io.ktor:ktor-server-cors:$ktor_version")
|
implementation("io.ktor:ktor-server-cors:$ktor_version")
|
||||||
|
implementation("io.ktor:ktor-utils:$ktor_version")
|
||||||
|
implementation("io.ktor:ktor-serialization-kotlinx-json:$ktor_version")
|
||||||
|
implementation("io.ktor:ktor-server-content-negotiation:$ktor_version")
|
||||||
|
implementation("io.ktor:ktor-client-content-negotiation:$ktor_version")
|
||||||
|
implementation("io.ktor:ktor-serialization-jackson:$ktor_version")
|
||||||
|
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.3")
|
||||||
}
|
}
|
10
backend/src/main/kotlin/no/nav/helse/sprik/Test.kt
Normal file
10
backend/src/main/kotlin/no/nav/helse/sprik/Test.kt
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
package no.nav.helse.sprik
|
||||||
|
|
||||||
|
class Test (
|
||||||
|
var ord: String,
|
||||||
|
var tall: Int
|
||||||
|
) {
|
||||||
|
override fun toString(): String {
|
||||||
|
return "Test(ord='$ord', tall=$tall)"
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,19 +1,39 @@
|
||||||
package no.nav.helse.sprik.plugins
|
package no.nav.helse.sprik.plugins
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.SerializationFeature
|
||||||
|
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule
|
||||||
import io.ktor.http.*
|
import io.ktor.http.*
|
||||||
|
import io.ktor.serialization.jackson.*
|
||||||
import io.ktor.server.routing.*
|
import io.ktor.server.routing.*
|
||||||
import io.ktor.server.response.*
|
import io.ktor.server.response.*
|
||||||
import io.ktor.server.application.*
|
import io.ktor.server.application.*
|
||||||
|
import io.ktor.server.plugins.contentnegotiation.*
|
||||||
import io.ktor.server.plugins.cors.routing.*
|
import io.ktor.server.plugins.cors.routing.*
|
||||||
|
import io.ktor.server.request.*
|
||||||
|
import no.nav.helse.sprik.Test
|
||||||
|
|
||||||
fun Application.configureRouting() {
|
fun Application.configureRouting() {
|
||||||
install(CORS) {
|
install(CORS) {
|
||||||
anyHost()
|
anyHost()
|
||||||
allowMethod(HttpMethod.Get)
|
allowMethod(HttpMethod.Get)
|
||||||
|
allowMethod(HttpMethod.Post)
|
||||||
|
//allowHeader(HttpHeaders.AccessControlAllowOrigin)
|
||||||
|
allowNonSimpleContentTypes = true
|
||||||
|
}
|
||||||
|
install(ContentNegotiation) {
|
||||||
|
jackson {
|
||||||
|
disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)
|
||||||
|
registerModule(JavaTimeModule())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
routing {
|
routing {
|
||||||
get("/") {
|
get("/") {
|
||||||
call.respondText("Hello World!")
|
call.respondText("Hello World!")
|
||||||
}
|
}
|
||||||
|
post("/test") {
|
||||||
|
val test = call.receive<Test>()
|
||||||
|
println(test)
|
||||||
|
//call.respondText(test.toString())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import "@navikt/ds-css";
|
import "@navikt/ds-css";
|
||||||
import { Button, Heading } from "@navikt/ds-react";
|
import { Button, Heading } from "@navikt/ds-react";
|
||||||
import axios , { Axios, AxiosError } from "axios";
|
import axios , { Axios, AxiosError } from "axios";
|
||||||
|
import { log } from "console";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
|
|
||||||
const fetcher = (url: any) => axios.get(url).then(res => res.data)
|
const fetcher = (url: any) => axios.get(url).then(res => res.data)
|
||||||
|
@ -10,6 +11,23 @@ export default function Home() {
|
||||||
const {data, error, isLoading} = useSWR('http://0.0.0.0:8080/', fetcher);
|
const {data, error, isLoading} = useSWR('http://0.0.0.0:8080/', fetcher);
|
||||||
console.log(data);
|
console.log(data);
|
||||||
|
|
||||||
|
function post() {
|
||||||
|
axios.post("http://0.0.0.0:8080/test", {
|
||||||
|
ord: "heisann hoppsann",
|
||||||
|
tall: 7
|
||||||
|
}, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
}).then( (response) => {
|
||||||
|
console.log(response)
|
||||||
|
}).catch( (error) => {
|
||||||
|
console.log(error);
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
post()
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main className="flex justify-center">
|
<main className="flex justify-center">
|
||||||
<div className="w-1/2 flex flex-col gap-4 justify-center text-center">
|
<div className="w-1/2 flex flex-col gap-4 justify-center text-center">
|
||||||
|
|
Reference in a new issue