https://kotlinlang.org logo
#http4k
Title
# http4k
d

dave

09/18/2019, 2:53 PM
if you use the API, it should handle all that for you
r

Razi Kheir

09/18/2019, 2:55 PM
Sorry not sure what you mean by that, I’m currnetly using OkHttp client and calling a service that uses spring. This will automatically be handled for me? (encoding parameters in URL)
d

dave

09/18/2019, 2:56 PM
when you construct your request, as long as you use the http4k API, all parts of the request will be encoded properly when it is sent over the wire. see this test to see how to do that: https://github.com/http4k/http4k/blob/master/http4k-core/src/test/kotlin/org/http4k/core/UriTest.kt
(strictly speaking that's the Uri that you're manipulating in that test, but you get the idea 🙂 )
r

Razi Kheir

09/18/2019, 3:03 PM
Thank you very much david!
d

dave

09/18/2019, 3:03 PM
np 🙂
r

Razi Kheir

09/18/2019, 3:18 PM
I don’t see this being done automatically https://github.com/http4k/http4k/blob/220ed20d61089ffea026c54f46c49c0d9d8b88ca/http4k-core/src/main/kotlin/org/http4k/core/Uri.kt Does this mean I need to call the static toPathEncoded when I add a uri?
Copy code
var ridUpdateUriPath = "<http://example.com/something/weird#@$@#$/member>"
Request(POST, ridUpdateUriPath)
When I debug and look at the request object uri, it still holds those characters, I wonder if I am initialising it incorrectly.
I also noticed this: https://github.com/http4k/http4k/blob/24e6f4357dc6b082ef9af58e56032da7f32fe057/http4k-core/src/main/kotlin/org/http4k/lens/path.kt#L80 Sorry if I’m miss undersanding, Im just wondering if my simple requested posted above will actually go through automatic url encoding
d

dave

09/18/2019, 3:34 PM
Copy code
import org.http4k.client.OkHttp
import <http://org.http4k.core.Method.POST|org.http4k.core.Method.POST>
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.Uri
import org.http4k.server.SunHttp
import org.http4k.server.asServer

fun main() {

    { re: Request ->
        println(re.uri)
        Response(OK)
    }.asServer(SunHttp()).start()

    val ridUpdateUriPath = "/something/weird /member"

    println(OkHttp()(Request(POST, Uri.of("<http://localhost>").port(8000).path(ridUpdateUriPath))))
}
if you run that you get
Copy code
/something/weird%20/member
HTTP/1.1 200 OK
date: Wed, 18 Sep 2019 15:34:24 GMT
transfer-encoding: chunked