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

elect

05/09/2021, 8:22 AM
Hi, I'm looking to some lib to replace crude shell commands such as
curl -X GET -H "Authorization: token $token <https://api.github.com/repos/kotlin-graphics/mary/contents/$path>
I also need to parse the responding json to extract, for example, a sha encoding and create some other json to pass to some other
curl
commands. Can http4k be all I need? 😛
d

dave

05/09/2021, 8:41 AM
I should certainly hope so! There is also a "toCurl()" function on Request so you can convert any object into the equivalent command to run it locally. 😉
e

elect

05/09/2021, 8:41 AM
you just got me
d

dave

05/09/2021, 8:43 AM
And if you want to capture traffic, you just can just use a simple traffic filter to record the requests and serialise them into disk as they get sent, or to replay it from disk later. Here the example: https://www.http4k.org/cookbook/record_and_replay/
e

elect

05/09/2021, 8:57 AM
how may I access to the json in return? On the terminal I get the following
Copy code
elect@5800x:~/IdeaProjects$ curl -X GET <https://api.github.com/repos/kotlin-graphics/mary/contents/mattei.txt>
{
  "name": "mattei.txt",
  "path": "mattei.txt",
  "sha": "3f2036880b9e1cea10c06df7dccdb47179e82fcc",
  "size": 5,
  "url": "<https://api.github.com/repos/kotlin-graphics/mary/contents/mattei.txt?ref=master>",
  "html_url": "<https://github.com/kotlin-graphics/mary/blob/master/mattei.txt>",
  "git_url": "<https://api.github.com/repos/kotlin-graphics/mary/git/blobs/3f2036880b9e1cea10c06df7dccdb47179e82fcc>",
  "download_url": "<https://raw.githubusercontent.com/kotlin-graphics/mary/master/mattei.txt>",
  "type": "file",
  "content": "cHJvdmE=\n",
  "encoding": "base64",
  "_links": {
    "self": "<https://api.github.com/repos/kotlin-graphics/mary/contents/mattei.txt?ref=master>",
    "git": "<https://api.github.com/repos/kotlin-graphics/mary/git/blobs/3f2036880b9e1cea10c06df7dccdb47179e82fcc>",
    "html": "<https://github.com/kotlin-graphics/mary/blob/master/mattei.txt>"
  }
}
d

dave

05/09/2021, 8:59 AM
well the body is available through
response.bodyString()
, but if you want actual JSON parsing/deserialisation, then you'll have to bring in a JSON module. Are you looking for a String, a JsonNode, or a deserialised object?
e

elect

05/09/2021, 9:00 AM
for the moment I'd just like to extract
"sha"
value
bodyString
returns an empty string
d

dave

05/09/2021, 9:01 AM
it will return an empty string only if something is already consuming the body stream.
(or there is no content)
e

elect

05/09/2021, 9:02 AM
Copy code
fun Project.http4k() {
    val token = project.property("githubToken")
    val request = Request(GET, "<https://api.github.com/repos/kotlin-graphics/mary/contents/mattei.txt>")
        .header("Authorization", "token $token")
    println(request.bodyString())
    println(request.toCurl())
}
first
println
is empty, second
curl -X GET -H "Authorization:token .." "https://api.github.com/repos/kotlin-graphics/mary/contents/mattei.txt"
d

dave

05/09/2021, 9:02 AM
you haven't actually made the request yet!
you need something like
JavaHttpClient()(request)
e

elect

05/09/2021, 9:06 AM
just tried that, I get
e: /home/elect/IdeaProjects/magik/plugin/src/main/kotlin/magik/MagikPlugin.kt: (172, 5): Cannot access class 'java.net.http.HttpClient'. Check your module classpath for missing or conflicting dependencies
I'm using http4k on a gradle plugin imported via composite build, importing as README
Copy code
implementation(platform("org.http4k:http4k-bom:4.8.0.0"))
    implementation("org.http4k:http4k-core")
    implementation("org.http4k:http4k-server-netty")
    implementation("org.http4k:http4k-client-apache")
d

dave

05/09/2021, 9:08 AM
that is definitely a problem with your java environment/gradle setup. If you're on Java8, you might need
Java8HttpClient
instead
e

elect

05/09/2021, 9:08 AM
what's
JavaHttpClient
instead? Java 6?
d

dave

05/09/2021, 9:08 AM
that's the one that is built into Java 11
(or 9+ I think, but 11 is the LTS)
e

elect

05/09/2021, 9:09 AM
weird, I was on 15
anyway,
Java8HttpClient
seems to not generate any errors, but the
::bodyString
still empty, am I missing something else?
Copy code
val request = Request(GET, "<https://api.github.com/repos/kotlin-graphics/mary/contents/mattei.txt>")
        .header("Authorization", "token $token")
    Java8HttpClient()(request)
    println(request.bodyString())
    println(request.toCurl())
d

dave

05/09/2021, 9:11 AM
yes 🙂 .
println(request.bodyString())
prints out the body of the request and not the response
Copy code
val response = Java8HttpClient()(request)
println(response.bodyString())
e

elect

05/09/2021, 9:11 AM
uh, sorry 😛
nice, thanks david
👍 1
btw, shall I also
response.close()
?
d

dave

05/09/2021, 9:16 AM
to be on the safe-side, you can, but it really depends on what you're doing with it - Response is also AutoCloseable, so you can just do
response.use {}
e

elect

05/09/2021, 9:18 AM
yep, I switched to that. I'm creating/updating files on a github repo. If the i-th file exists, I have to pass its sha first, which is why I have to query that first
sorry to bother you again, but I can find easily how I can add the following
Copy code
-d "{\"path\": \"mattei.txt\", \"message\": \"TEST-9876 add metadatafile\", \"content\": \"cHJvdmE=\", \"branch\": \"master\", \"sha\": \"3f2036880b9e1cea10c06df7dccdb47179e82fcc\"}"
it should be the body, isn't it?
d

dave

05/09/2021, 9:27 AM
yep
request.body("""{"path": "mattei.txt", "message": "TEST-9876 add metadatafile", "content": "cHJvdmE=", "branch": "master", "sha": "3f2036880b9e1cea10c06df7dccdb47179e82fcc"}""")
e

elect

05/09/2021, 9:33 AM
nice, thanks, you were really useful and immediate david 😉
3 Views