Using cURL to authenticate with JWT Bearer tokens

What I learned today — 29 August 2018

Postman doesn’t have nice support for authenticating with an API that uses simple JWT authentication and Bearer tokens. Whatever the question, cURL is usually the answer.

Get the Bearer token using cURL and jq

TOKEN=$(curl -s -X POST -H 'Accept: application/json' -H 'Content-Type: application/json' --data '{"username":"{username}","password":"{password}","rememberMe":false}' https://{hostname}/api/authenticate | jq -r '.id_token')

In this example the API expects a POST body with “username”, “password” and “rememberMe” fields. Adapt according to your own needs.

jq is used to parse the JSON response, which contains the token in a field called “id_token”.

Pass the Bearer token in the Authorization header

curl -H 'Accept: application/json' -H "Authorization: Bearer ${TOKEN}" https://{hostname}/api/myresource

Written by

Full-stack developer at South African blockchain startup

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store