I have a request that works with curl at the comma...
# http4k
e
I have a request that works with curl at the command line:
Copy code
curl -A "ellens-user-agent" -X POST -d 'grant_type=password&username=USER&password=PASSWORD' --user 'SCRIPTID:SECRET' <https://www.reddit.com/api/v1/access_token>
How do I convert that into an
http4k
request?
a
Copy code
val request = Request(<http://Method.POST|Method.POST>, "<https://www.reddit.com/api/v1/access_token>")
    .withBasicAuth(Credentials("SCRIPTID", "SECRET"))
    .header("User-Agent", "ellens-user-agent")
    .body("grant_type=password&username=USER&password=PASSWORD")
-A
is just a shortcut to set the
User-Agent
header
--user
is just a shortcut to provide credentials for the authentication challenge; in this case, we just want basic
-d
just sets the HTTP body. In this case, I'm sending it as a string, but http4k has controls for typesafe form bodies: https://www.http4k.org/guide/howto/use_html_forms/
e
Thanks! I should have mentioned it was the
--user
field that I didn't know how to convert.
I haven't been able to find nontrivial examples of using
http4k
as a client of an existing API.
a
Once you start getting the hang of authorizing and sending requests, you might want to swap this request out for the built-in oauth security filters. https://www.http4k.org/guide/reference/oauth/
Because all that request is doing is performing the OAuth 2.0 Resource-Owner Password grant
e
Thanks for the advice. I want to interface with the Reddit API, which may be a mistake because they don't have an OpenAPI spec, just the one shown. https://www.reddit.com/dev/api
a
OpenAPI specs are nice, but I'm sure you can build a custom client.
e
I'm thinking I'll switch to a different API since I'm teaching a class and am creating an assignment for students. I think it would be more useful to show them how to integrate with a compliant API than implement a special case (which I'd have to do).
a
httpbin has a spec. Very useful playground for testing requests. https://httpbin.org/#/
s
A couple of minor additions to @Andrew O'Hara’s answer:
Copy code
val request = Request(<http://Method.POST|Method.POST>, "<https://www.reddit.com/api/v1/access_token>")
    .withBasicAuth(Credentials("SCRIPTID", "SECRET"), "Authorization")
    .header("User-Agent", "ellens-user-agent")
    .form("grant_type", "password")
    .form("username", "USER")
    .form("password", "PASSWORD")
One can use
.form
to define each data key pair individually. Also, you can use
request.toCurl()
to see the curl version of that request and check/run the request in the command line. For the example above it generates:
Copy code
curl -X POST -H "Authorization:Basic U0NSSVBUSUQ6U0VDUkVU" -H "User-Agent:ellens-user-agent" --data "grant_type=password&username=USER&password=PASSWORD" "<https://www.reddit.com/api/v1/access_token>"
j
Hey Ellen. It is very interesting you are using http4k in a class... are you able to share a bit on what sort of class it is? Maybe there are already some materials that could help.. or maybe we could collate some?
a
@s4nchez I had no idea there were form shortcuts like that. Good to know!
e
I ended up using someone else's Java wrapper for Reddit rather than making my own, but thanks for the help.