I’m having trouble sending POST requests with Authorization: Bearer <token>
headers to my WordPress site. The issue seems to be server-related since everything works perfectly in Postman.
My Setup
I’m running MAMP Pro with a WordPress site that has a custom API (not the default WordPress REST API).
The Process
- Login request from
client-app.com/auth.html
toserver-api.com/v1/login
with credentials. - Get back a JWT token named
accessToken
. - Send data from
client-app.com/dashboard.html
toserver-api.com/v1/notifications
with form data andAuthorization: Bearer <accessToken>
header.
Login Code (Step 1):
const BASE_URL = "https://server-api.com";
const LOGIN_ENDPOINT = "v1/login";
const NOTIFICATION_ENDPOINT = "v1/notifications";
const HTTP_METHOD = "POST";
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open(HTTP_METHOD, BASE_URL + "/" + LOGIN_ENDPOINT);
xhr.onload = () => resolve(xhr.response);
xhr.onerror = () => reject(xhr.response);
xhr.send(loginData);
});
Protected Request Code (Step 3):
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open(HTTP_METHOD, BASE_URL + "/" + NOTIFICATION_ENDPOINT);
xhr.setRequestHeader('Authorization', "Bearer " + TokenService.getAccessToken());
xhr.onload = () => resolve(xhr.status);
xhr.onerror = () => reject(xhr.status);
xhr.send(notificationData);
});
The Problem
Step 1 works fine everywhere. Step 3 works in Postman but fails the preflight OPTIONS request in the browser. I tested with a third-party mock API and it worked, so I think it’s my server config.
What I’ve Tried
- Added rewrite rules to MAMP virtual host and .htaccess.
- Removed Authorization header (request works without it).
- Set different Content-Type headers.
- Added trailing slashes to endpoints.
- Various SetEnvIf configurations.
I’m stuck and think either Apache is blocking Authorization headers or WordPress is interfering with CORS. Any ideas?