ktor-swagger icon indicating copy to clipboard operation
ktor-swagger copied to clipboard

post<location,body> Always returns HTTP 415

Open devrimaksakal opened this issue 6 years ago • 7 comments

I am able to test get methods (via curl and swagger client) with no issues. but post method never parse the json body. Always returns HTTP 415 Unsupported Media error.

@Group("Debug Group") @Location("/RefData/Xceptor/v1/findIspreadByIdList/x") class testLocation()

post<testLocation, TestModel>("find Ispread by Id(s)".responds(ok<InterceptorClientResponse>()).header<Header>().examples(example("body", TestModel.exampleBody, summary = "Map of name and age"))){ _ , postBody->

...}

class TestModel(val name : String, val age: String ){
    companion object {
        val exampleBody = mapOf(
            "Alex" to "25"
        )
    }

body:

{
  "name":"Alex",
  "age":"25"
}

Thanks in advance..

devrimaksakal avatar Jan 30 '19 16:01 devrimaksakal

Do you have a stack trace? Also, please use GitHub Markdown formatting for your issues (I updated your issue for you above), it makes it far more readable.

JLLeitschuh avatar Jan 30 '19 18:01 JLLeitschuh

Apologies, naive in Github markdown. Tried to format this one.. I have call trace. No stack trace...

2019-01-30 13:12:38.936 [nettyCallPool-4-1] TRACE Application - Trace for [RefData, finder, v1, findByIdList, x]
/, segment:0 -> SUCCESS @ /TestData/finder/v1/findByIdList/x/(method:POST))
  /apidocs, segment:0 -> FAILURE "Selector didn't match" @ /apidocs)
  /(method:GET), segment:0 -> FAILURE "Selector didn't match" @ /(method:GET))
  /TestData, segment:1 -> SUCCESS @ /TestData/finder/v1/findByIdList/x/(method:POST))
    /TestData/finder, segment:2 -> SUCCESS @ /TestData/finder/v1/findByIdList/x/(method:POST))
      /TestData/finder/v1, segment:3 -> SUCCESS @ /TestData/finder/v1/findByIdList/x/(method:POST))
        /TestData/finder/v1/getRoutes, segment:3 -> FAILURE "Selector didn't match" @ /TestData/finder/v1/getRoutes)
        /TestData/finder/v1/findById, segment:3 -> FAILURE "Selector didn't match" @ /TestData/finder/v1/findById)
        /TestData/finder/v1/findByIdList, segment:4 -> SUCCESS @ /TestData/finder/v1/findByIdList/x/(method:POST))
          /TestData/finder/v1/findByIdList/(method:POST), segment:4 -> FAILURE "Not all segments matched" @ /TestData/finder/v1/findByIdList/(method:POST))
          /TestData/finder/v1/findByIdList/x, segment:5 -> SUCCESS @ /TestData/finder/v1/findByIdList/x/(method:POST))
            /TestData/finder/v1/findByIdList/x/(method:POST), segment:5 -> SUCCESS @ /TestData/finder/v1/findByIdList/x/(method:POST))
  /{...}, segment:0 -> FAILURE "Better match was already found" @ /{...})

2019-01-30 13:12:38.992 [nettyCallPool-4-1] TRACE Application - 415 Unsupported Media Type: POST - /TestData/finder/v1/findByIdList/x

devrimaksakal avatar Jan 30 '19 18:01 devrimaksakal

You need to install one of the converters to convert your JSON to the TestModel. The simplest way to do that is to either use the ktor plugin for Gson or Jackson.

JLLeitschuh avatar Jan 30 '19 18:01 JLLeitschuh

I have this in the module:

 install(ContentNegotiation) {
        gson {
            setPrettyPrinting()
            setDateFormat(LocalDate.now().toString())
        }
        register(ContentType.Any, GsonConverter())
    }

but post is located in another file

devrimaksakal avatar Jan 30 '19 18:01 devrimaksakal

Update: In the same handler file when i put another post method (io.ktor.locations.post) and send the same json in the body, it parse and process the post request just fine. Issue is not json parsing/serializing. Something around the library, either the way i implemented (which i followed the example) or the library has issues.

devrimaksakal avatar Jan 30 '19 21:01 devrimaksakal

I think your next step is to explore this in a debugger and see where you are getting the 415 from.

Sorry I can't be of more assistance, but I don't really have enough information at the moment to be of more help.

JLLeitschuh avatar Jan 30 '19 21:01 JLLeitschuh

Thank you I found the problem. The issue with complex data structures parsing/mapping/serializing: when I try to put a Map<String, List<String>> equivalent json in the body, it cannot serialize into custom data object as per example (tried wrapper objects to, no luck). It cannot even serialize into Map<Any, Any>. So, I had to accept as Any and then build the json out of that. The confusing part was that whatever the exception is in the post, it always return Http 415 error. Thank you...

devrimaksakal avatar Jan 31 '19 12:01 devrimaksakal