I have been wrangling with Fat Free and Fetch for the past couple of hours. I think sharing these notes will be helpful for others who may have the same issues. I also think the documentation might need to addresses this, and perhaps even the core framework might want to adjust for this.
When POSTing data with the JS Fetch API, F3 will not populate the POST global unless the content type is application/x-www-form-urlencoded
An example with BLANK $f3->get('POST')
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ sig: sigImage })
};
fetch("/api/endpoint", requestOptions)
.then(response => response.json())
.then(data => console.log(data) );
});
An example with working $f3->get('POST')
const requestOptions = {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: JSON.stringify({ sig: sigImage })
};
fetch("/api/endpoint", requestOptions)
.then(response => response.json())
.then(data => console.log(data) );
});
Actually its a limitation (feature?) of PHP and anything that is POSTed that isnt "x-www-form-urlencoded" can't be reacd without $x = file_get_contents("php://input")
As I mentioned I think there can be a significant value add to the framework (for tiny price) to automatically handle these kinds of requests as they are only getting more common... (Or at least a note in the official documentation.)
Any thoughts? Thanks for a great Framework providing decades of productivity for many of us!