So my end goal here is to get a post request which...
# server
j
So my end goal here is to get a post request which passes JSON data into my backend.I’m getting this error(caught in catch block):
Copy code
Cannot transform this request's content to com.company.plugins.Articles
I’ve setup content negotiation and resources as such
Copy code
import io.ktor.http.*
import io.ktor.resources.*
import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.*
import io.ktor.server.plugins.contentnegotiation.*
import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.request.*
import io.ktor.server.resources.*
import io.ktor.server.resources.Resources
import <http://io.ktor.server.resources.post|io.ktor.server.resources.post>
import io.ktor.server.response.*
import io.ktor.server.routing.*
import kotlinx.serialization.Serializable
fun Application.plugins() {
install(ContentNegotiation) {
        json()
        println("We have installed content negotiation with json()")
    }
    install(Resources)
And verified that the println shows up in the console logs. I’ve got the API setup as such:
Copy code
@Serializable
@Resource("/articles")
data class Articles(val sort: String? = "new on no") {

    @Serializable
    @Resource("new")
    class New(val parent: Articles = Articles())

    @Serializable
    @Resource("{id}")
    class Id(val parent: Articles = Articles(), val id: Long) {
        @Serializable
        @Resource("edit")
        class Edit(val parent: Id)
    }
}
fun Application.configureRouting() {
println("routing")
    // Starting point for a Ktor app:
    routing {
        post<Articles.New> { article ->
            try {
                var arty = call.receive<Articles>()
                println("no excpetionsf")
            } catch (e: Exception) {
                println("exception")
                println(e.message)
            }
            call.respondText("Create a new article")
        }
    }
}
And am making the request as such:
Copy code
function sendPostRequest() {
      fetch('<http://localhost:8080/articles/new>',
        {
          headers: {
            'Content-Type': 'application/json',
            'Accept': 'application/json'
          },
          mode: 'no-cors',
          method: "POST",
          body: JSON.stringify({sort: "Jet", parent: "woooooooooo", numba: 1})
        })
        .then(response => response.text())
        .then(data => {
          console.log('Success:', data);
        })
        .catch((error) => {
          console.error('Error:', error);
        });
  }
🧵 2