POST request in a route

Try:

'headers' => array(
        'Content-Type: application/json;charset=ISO-8859-1',
        'Authorization: Basic SECRET'
      ),

Bingo :slight_smile: Succesful response… thanks.

Now i just need to get fetch to send that information through to the route… hang on to your hat!

Hrmm…

When i try and fetch it in javascript, i get a SyntaxError: "JSON.parse: unexpected character at line 1 column 1 of the JSON data". I know the route works becuase i get json back when i hit in the browser.

Heres my fetch request…

const url = 'http://domain.test/createsession.json';
// The data
let data = JSON.stringify({
    amount: '5',
    id: <?= $transidformat ?>
});
// The parameters
let fetchData = {
    method: 'POST',
    body: data,
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    },
}
fetch(url, fetchData)
.then(response => {
  return response.json();
})
.then(data => {
// Work with JSON data here
console.log(data);
})
.catch(err => {
// Do something for an error here
console.log(err);
});