Ktor सर्वर पोस्टमैन में 406 त्रुटि के साथ प्रतिक्रिया करता है


मैं एक बैकएंड सर्वर कर रहा हूं और रूट का परीक्षण/रजिस्टर करते समय मुझे कुछ परेशानी हो रही है। मुझे लगता है कि मैंने सब कुछ सही तरीके से कॉन्फ़िगर किया है, लेकिन जब मैं पोस्टमैन पर POST विधि करने का प्रयास करता हूं तो यह मुझे 406 त्रुटि देता है। यह वह लिंक है जिसका मैं उपयोग कर रहा हूं ´http://127.0.0.1:8080/v1/users/register´ यह मैं PostGreSQL DB से जुड़ा हूं, पोस्टमैन पर अनुरोध करने के बाद मैंने डेटाबेस की भी जांच की, लेकिन वहां कुछ भी नहीं है, इस रूटिंग भाग से पहले मैंने पहले ही Ktor का उपयोग करके एक तालिका बना ली है।

यह मेरी रूट फ़ाइल है

Kotlin
fun Route.userRoutes(db: repo, jwtService: JwtService, hashFunction: (String) -> String) {
    route("/v1/users") {
        post("/register") {
            try {
                val registerRequest = call.receive<RegisterRequest>()

                // Handle registration logic
                val user = User(registerRequest.email, hashFunction(registerRequest.password), registerRequest.name)
                db.addUser(user)
                call.respond(HttpStatusCode.OK, SimpleResponse(true, jwtService.generateToken(user)))

            } catch (e: Exception) {
                // Handle registration errors
                call.respond(HttpStatusCode.BadRequest, SimpleResponse(false, "Missing Some Fields or Registration Failed"))
            }
        }

कक्षा पंजीकृत करें

Kotlin
data class RegisterRequest(
    val email:String,
    val name:String,
    val password:String
)

सरल प्रतिक्रिया वर्ग

Kotlin
package com.example.data.model

data class SimpleResponse(
    val success:Boolean,
    val message:String
)

और जब मैं पोस्टमैन पर पोस्ट भेजने का प्रयास करता हूं तो IntelliJ में मेरे कंसोल पर यही दिखाई देता है

टर्मिनल
2023-12-29 21:42:28.514 [eventLoopGroupProxy-4-1] TRACE io.ktor.server.routing.Routing - Trace for [v1, users, register]
/, segment:0 -> SUCCESS @ /
  /session, segment:0 -> FAILURE "Selector didn't match" @ /session
  /json, segment:0 -> FAILURE "Selector didn't match" @ /json
  /, segment:0 -> SUCCESS @ /
    /(method:GET), segment:0 -> FAILURE "Selector didn't match" @ /(method:GET)
  /v1, segment:1 -> SUCCESS @ /v1
    /v1/users, segment:2 -> SUCCESS @ /v1/users
      /v1/users/register, segment:3 -> SUCCESS @ /v1/users/register
        /v1/users/register/(method:POST), segment:3 -> SUCCESS @ /v1/users/register/(method:POST)
      /v1/users/login, segment:2 -> FAILURE "Selector didn't match" @ /v1/users/login
Matched routes:
  "" -> "v1" -> "users" -> "register" -> "(method:POST)"
Route resolve result:
  SUCCESS @ /v1/users/register/(method:POST)
2023-12-30 21:04:26.349 [eventLoopGroupProxy-4-1] TRACE i.k.server.engine.DefaultTransform - No Default Transformations found for class io.ktor.utils.io.ByteBufferChannel and expected type TypeInfo(type=class com.example.data.model.RegisterRequest, reifiedType=class com.example.data.model.RegisterRequest, kotlinType=com.example.data.model.RegisterRequest) for call /v1/users/register
2023-12-30 21:04:26.375 [eventLoopGroupProxy-4-1] TRACE i.k.s.p.c.ContentNegotiation - No suitable content converter found for request type class com.example.data.model.RegisterRequest
2023-12-30 21:04:26.384 [eventLoopGroupProxy-4-1] TRACE io.ktor.server.sessions.Sessions - Sending session data for /v1/users/register: MY_SESSION
2023-12-30 21:04:26.407 [eventLoopGroupProxy-4-1] TRACE i.k.s.p.c.ContentNegotiation - No suitable content converter found for response type class com.example.data.model.SimpleResponse and body SimpleResponse(success=false, message=Missing Some Fields)

अनुरोध पर निकाय यह है

{
“नाम”: “जॉन”,
“ईमेल”: “abcd@gmail.com”,
“पासवर्ड”: “पासवर्ड”
}

Kotlin
fun Application.configureSerialization() {

    install(ContentNegotiation) {

            Json {
                prettyPrint = true
                isLenient = true
                encodeDefaults = false
            }

    }

    routing {
        get("/json/gson") {
            call.respond(mapOf("hello" to "world"))
        }
    }
}

मैंने क्या प्रयास किया है:

I have checked the headers tab on Postman and I have the Content-Type application/json and also the Accept "/"

The dependencies on Gradle I used latest.release

This is just for a simple Note App for android.

The problem seems to be because the RegisterRequest and SimpleResponse I think.

This is my first time using Ktor so sorry if I'm a bit behind in some aspects. Thank you for your time

समाधान 1

मैं कोटलिन को बिल्कुल नहीं जानता, लेकिन ऐसा लगता है कि आप एक अपवाद को पकड़ रहे हैं लेकिन इसे पूरी तरह से अनदेखा कर रहे हैं, केवल एक सामान्य प्रतिक्रिया दे रहे हैं:

Kotlin
} catch (e: Exception) {
    // Handle registration errors
    call.respond(HttpStatusCode.BadRequest, SimpleResponse(false, "Missing Some Fields or Registration Failed"))

अपवाद संदेश को वापस करने का प्रयास करें, या कहीं लॉग इन करें ताकि आपको बेहतर अंदाज़ा हो सके कि अपवाद किस बारे में शिकायत कर रहा है।

コメント

タイトルとURLをコピーしました