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

Zoltan Demant

09/26/2023, 1:42 PM
Im seeing
Unhandled redirect: DELETE <https://foo.bar>. Status: 301 Moved Permanently. Text: ""
for certain requests. Yet, the URL:s work perfectly fine when using curl in the command line. Why could that be?
c

Casey Brooks

09/26/2023, 1:51 PM
Is your client engine configured to follow redirects? https://ktor.io/docs/http-redirect.html If you’re using the OkHttp engine, you might check that configuration as well. By default, OkHttp doesn’t follow redirects, so you may need to enable it there before passing the OkHttp instance to Ktor https://square.github.io/okhttp/4.x/okhttp/okhttp3/-ok-http-client/-builder/follow-redirects/
z

Zoltan Demant

09/26/2023, 2:21 PM
Furthermore, I have the following specified
followRedirects = true
(forgot to press send earlier 😅).
Im also pretty sure that the response isnt a redirect, or at least I cant see why it would be. Is it possible to debug that part?
c

Casey Brooks

09/26/2023, 2:30 PM
A 301 status code indicates a permanent redirect, and per the HTTP spec it’s required that it includes a
Location
header with the URL to redirect to. It’s commonly used to upgrade an HTTP connection to HTTPS, for example. https://en.wikipedia.org/wiki/HTTP_301 Also, by default it looks like the Ktor client only handles redirection for GET requests, while you’re is a DELETE. You might try configuring it to set this property to false so it also works for DELETE
z

Zoltan Demant

09/26/2023, 3:42 PM
Ill try adjusting the property tomorrow morning! Is it possible that theres a redirect happening even though the URL:s are identical?
c

Casey Brooks

09/26/2023, 3:44 PM
I’m not too sure what exactly the Ktor engine’s logic is, but I could imagine a strange situation where the server might issue a redirect to the same URL with some additional cookies or other headers
z

Zoltan Demant

09/27/2023, 3:42 AM
It works after changing the property, thank you!
Please note: changing this flag could lead to security issues, consider changing the request URL instead.
I dont believe this is possible for me to do, given that theyre identical on the surface? Also, never thought about the server adding cookies/headers like that - but makes sense, Ill check with the API owners about this.
Apparently, missing a
/
at the end of the URL makes a world of a difference! I cant believe I missed that, looking through my logs I can see that the redirect URL actually has the added slash, I just didnt notice it .. for 5 hours yesterday ❤️ Everything works now.
c

Casey Brooks

09/27/2023, 2:12 PM
Ah, yeah adding (or removing) a trailing slash is another common situation for redirects, especially when the server is something like Wordpress or another PHP CMS
💪🏽 1