https://kotlinlang.org logo
#ktor
Title
# ktor
m

Matt Kranzler

07/10/2020, 2:24 PM
I'm having a strange issue which I believe is possibly a bug in ktor but I'm struggling to figure out what could be happening. I have a local http server running a spring boot rest api and I'm attempting to make a series of http requests via the Android emulator using ktor in a shared common module. The requests look like this:
Copy code
POST -> <http://10.0.2.2:8080/account/login>
GET -> <http://10.0.2.2:8080/user/me>
POST -> <http://10.0.2.2:8080/user/devices>
The first two requests succeed but the third request is returning a 500 from my server with the following error:
Copy code
org.springframework.security.web.firewall.RequestRejectedException: The request was rejected because the HTTP method "ST" was not included within the whitelist [HEAD, DELETE, POST, GET, OPTIONS, PATCH, PUT]
I've debugged the earliest entry point on the server side and verified the HTTP method is coming in as
ST
. I've also enabled tomcat access logging and verified the request comes in with an HTTP method of
ST
. On the Android/multiplatform side the ktor logging show the last request as a
POST
and the Android Studio network profiler show the request as a
POST
. What's even more weird is if I change the order of the requests to this:
Copy code
POST -> <http://10.0.2.2:8080/account/login>
POST -> <http://10.0.2.2:8080/user/devices>
GET -> <http://10.0.2.2:8080/user/me>
it works fine and comes through as a
POST
on the server side. What makes this even more difficult to track down is that I'm unable to debug on the client side because as soon as I run with the debugger or attach the debugger Android Studio and Intellij both lock up completely and I have to force quit. Does anyone have any ideas on what could be happening or any suggestions on what else to try?
n

nschulzke

07/10/2020, 3:30 PM
Have you tried running those requests as tests, instead of through the emulator? If you can eliminate Android/the emulator as a problem, that would help to narrow down what your problem might be.
m

Matt Kranzler

07/10/2020, 3:48 PM
@nschulzke yes, I've tried running the same code in a jvm test and I get the same error on the server side
n

nschulzke

07/10/2020, 3:49 PM
https://github.com/Moya/Moya/issues/1533 Read through this thread. It looks like this may be a Spring problem, as a lot of people were having the exact same issue (first two characters missing from the method) using Moya on iOS.
(And they were also using Spring)
m

Matt Kranzler

07/10/2020, 3:53 PM
Yeah, that does look very similar to what I'm seeing. I'll continue looking at the spring side and see if I can get anywhere
I finally figured out what was going on and figured I'd post here since it was tricky to track down. So I was setting a
Content-Type
header on the GET request of
application/json
which was causing ktor to serialize an empty body to
{}
. My server wouldn't process the body since it wasn't expecting it and then the next request would be made and the server would throw out the first two bytes resulting in the http method getting chopped off.
n

nschulzke

07/16/2020, 3:47 PM
That's nuts! It makes so much sense now that you describe it, but I'm impressed you finally figured it out.
m

Matt Kranzler

07/16/2020, 4:15 PM
Yeah, it wasn't easy to figure out especially since I wasn't able to debug the client side (every time I attached the debugger Intellij would lockup and needed to force quit). Once I finally got that working it was a bit easier to track down
86 Views