https://kotlinlang.org logo
#getting-started
Title
# getting-started
g

grace

10/29/2023, 8:08 AM
hii im trying the backend framework http4k using this tutorial https://www.http4k.org/guide/tutorials/your_first_http4k_app/ it says on step5:
Try running the client by hitting the little green arrow on line 11. You'll see the
Response
printed to the console by the
Filter
, followed by a repeat of the body content, which is printed by line 18.
but Im not able to see any response. I have also run my server. even if my server is not running, it should show some error like server unavailable, no? but it just hangs there I think. I even add a few logs to try debug. pls help thanks
s

s4nchez

10/29/2023, 8:55 AM
Try this:
Copy code
import org.http4k.client.JavaHttpClient
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.then
import org.http4k.filter.DebuggingFilters.PrintResponse
import org.http4k.server.SunHttp
import org.http4k.server.asServer

fun main() {
    val server = { _: Request -> Response(OK).body("pong") }
    val runningServer = server.asServer(SunHttp(9000)).start()

    val client = JavaHttpClient()
    val printingClient = PrintResponse().then(client)

    printingClient(Request(Method.GET, "<http://localhost:9000/ping>"))

    runningServer.stop()
}
That code prints:
Copy code
***** RESPONSE 200 to GET: <http://localhost:9000/ping> *****
HTTP/1.1 200 
content-length: 4
date: Sun, 29 Oct 2023 08:54:56 GMT

pong

Process finished with exit code 0
You can also check out this gist showing how to separate the server and client applications
g

grace

10/29/2023, 9:04 AM
thx but still not working for me😩
but i can call the server on my chrome
s

s4nchez

10/29/2023, 9:07 AM
I can't spot anything wrong in the code. Can you try it on a different port?
g

grace

10/29/2023, 9:08 AM
oo yes, it works. thx:)
🎉 1
💪 1
🙂 1
s

s4nchez

10/29/2023, 9:08 AM
Yes, that's a subtle issue: in some operating systems you can have more than one process serving on the same port, and that's not always easy to spot.
That's why we also recommend making ports dynamic for testing, but for that you need to use another server backend that supports that. Here's the example above using Undertow instead of SunHttp:
Copy code
import org.http4k.client.JavaHttpClient
import org.http4k.core.Method
import org.http4k.core.Request
import org.http4k.core.Response
import org.http4k.core.Status.Companion.OK
import org.http4k.core.then
import org.http4k.filter.DebuggingFilters.PrintResponse
import org.http4k.server.SunHttp
import org.http4k.server.Undertow
import org.http4k.server.asServer

fun main() {
    val server = { _: Request -> Response(OK).body("pong") }
    val runningServer = server.asServer(Undertow(0)).start()

    val client = JavaHttpClient()
    val printingClient = PrintResponse().then(client)
    println("bb")
    printingClient(Request(Method.GET, "<http://localhost>:${runningServer.port()}/ping"))
    println("gogo")
    runningServer.stop()
}
This starts a server and calls it on a different port on every execution.
(it requires adding
org.http4k:http4k-server-undertow
as a dependency)
g

grace

10/29/2023, 9:11 AM
what do u mean? if its dynamic then how do we know if it keeps changing
s

s4nchez

10/29/2023, 9:12 AM
I mean for running locally and for testing, where you control both client and servers, you can make servers dynamic and orchestrate client creation so they know what to call.
For running in production (or any other deployed environment) you still need to make ports fixed and configurable. If you want to access it locally in your browser, you have a choice to either fix the server port, or make the server url visible during startup and point your browser to that.
2 Views