I was doing some work with the WordPress API, and I had to authenticate to perform a POST request to a website.

The easiest way for me was to use basic authentication.

I was using Axios, so I set the Authorization header to the POST request in this way:

const username = ''
const password = ''

const token = Buffer.from(`${username}:${password}`, 'utf8').toString('base64')

const url = 'https://...'

axios.post(url, {
  headers: {
    'Authorization': `Basic ${token}`

Download my free JavaScript Beginner's Handbook, and check out my upcoming Full-Stack JavaScript Bootcamp! A 4-months online training program. Signups now open, until June 1!.

Launching this week: