Inspection

When developing client applications, you will sometimes have trouble to understand, why you are getting a specific response from a webserver. The inspection concern allows you to add the query param ?inspect to any request and will respond with a detailed summary of how the received request looked like and what response was generated by the server.

Kotlin
import org.genhttp.engine.internal.Host

import org.genhttp.modules.inspection.*
import org.genhttp.modules.layouting.*
import org.genhttp.modules.practices.*

suspend fun main() {
    val app = Layout.create()
        .addInspector()

    Host.create()
        .handler(app)
        .defaults()
        .run()
}

When running this example and opening http://localhost:8080/some/request?inspect in your browser, you will get a summary of the request that was issued by the client (formatted and shortened here):

JSON
{
  "server": {
    "version": "11.1.0.3",
    "development": false,
    "handler": "org.genhttp.engine.shared.infrastructure.CoreRouter@69d3ecdf",
    "endpoints": [{ "port": 8080, "secure": false, "requestSource": true }]
  },
  "client": { "protocol": 0, "ipAddress": "127.0.0.1" },
  "request": {
    "protocol": "HTTP/1.1",
    "method": "GET",
    "path": "/some/request",
    "target": { "current": "some", "trailingSlash": false, "last": false },
    "headers": [
      { "key": "Host", "value": "localhost:8080" },
      { "key": "Accept", "value": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" },
      { "key": "Accept-Language", "value": "en-US,en;q=0.9" }
    ],
    "query": [{ "key": "inspect", "value": "" }]
  }
}

If the server generated a response for the request, the summary also contains its status, headers and content. With this concern in place, you can add the query parameter to the request generated by your client and convert the response sent by the server into a string to view the summary.

Custom Serialization

By default, the summary is serialized as JSON, or as YAML if the client asks for it with Accept: application/yaml. You can adjust this behavior by passing a custom serialization registry to the builder. The following example will always serialize to YAML, which is easier to read for humans:

Kotlin
import org.genhttp.api.protocol.ContentType

import org.genhttp.modules.conversion.Serialization
import org.genhttp.modules.conversion.serializers.yaml.YamlFormat
import org.genhttp.modules.inspection.*
import org.genhttp.modules.layouting.*

val serialization = Serialization.empty()
    .default(ContentType.ApplicationYaml)
    .add(ContentType.ApplicationYaml, YamlFormat())
    .build()

val inspection = Inspector.create().serialization(serialization)

val app = Layout.create()
    .add(inspection)