Mastodon hachyterm.io

I use curlie (for curl) and fish shell (for bash).

Original post by Niel de Wet: Using cURL to authenticate with JWT Bearer tokens.

Get The Bearer Token

You must install jq for parsing json from the command line.

The following command queries the authentication endpoint with the required credentials and stores the JWT bearer token in an environment variable.

set -x AUTH_TOKEN (curlie post https://hostname/auth/signin username='your username' password='your password' | jq -r '.accessToken')

That assumes that the json response from the API looks like this:

{ "accessToken": "blablabla" }

Query With Authorization Header

Now you can use other endpoints and send the auth token in the header. For example:

curlie get https://hostname/api -H "Authorization: Bearer $AUTH_TOKEN"

or

curlie post https://hostname/api -H "Authorization: Bearer $AUTH_TOKEN" title="Star Trek: Picard" year="2019" rating="7"

Further Reading