Ellen Spertus
11/13/2022, 9:29 PMcurl -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?Andrew O'Hara
11/13/2022, 10:20 PMval 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/Ellen Spertus
11/13/2022, 10:21 PM--user
field that I didn't know how to convert.Ellen Spertus
11/13/2022, 10:27 PMhttp4k
as a client of an existing API.Andrew O'Hara
11/13/2022, 10:31 PMAndrew O'Hara
11/13/2022, 10:31 PMEllen Spertus
11/13/2022, 10:32 PMAndrew O'Hara
11/13/2022, 10:34 PMEllen Spertus
11/13/2022, 10:36 PMAndrew O'Hara
11/13/2022, 10:41 PMs4nchez
11/14/2022, 7:55 AMval 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:
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>"
James Richardson
11/14/2022, 8:18 AMAndrew O'Hara
11/14/2022, 4:01 PMEllen Spertus
11/15/2022, 4:25 AM