UNIX Echo
21 Oct 2019 [ ]Today at work, I was testing an API for a new payments partner for TransferWise. The API’s authorisation required the keys to be encoded in base64. Something like:
curl -X POST https://api.example.com/bearer/ \
-H 'Authorization: Basic eW9tYW1hc29mYXQ=' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-d grant_type=client_credentials
I spent a good hour combing the documentation and understand if I was missing any headers in my API call. The error I was getting was 401, so it had to be related to the authorisation header. After a bit of combing through Google, I found out that the UNIX command echo actually appends a newline to its operand. So “jose:passw0rd” is actually “jose:passw0rd\n” when you do echo "jose:passw0rd" | base64
.
Luckily, it’s an easy fix and adding a -n
to the command stops echo from adding this sneaky newline, and thus correctly encoding the authorisation string!