If you’re using javascript to submit the form data to the REST endpoint, then you should handle the redirection at the client side, not at the server with PHP. Read the location header from the response and pass it to window.location
to redirect the user somewhere.
Here’s a simplified example – not tested, requires fleshing out.
document.getElementById('my-form').addEventListener('submit', (event) => {
event.preventDefault();
submitViaRestApi(event)
.then(response => redirectToLocation(response.location))
.catch(error => console.error(error));
});
function redirectToLocation(location) {
// like a mouse click
// window.location.href = location;
// or
// a HTTP redirect
// window.location.replace(location);
// Learn about window.location at https://developer.mozilla.org/en-US/docs/Web/API/Location
}
function submitViaRestApi(event) {
return new Promise((resolve, reject) => {
// send data to REST endpoint
});
}