I'm using a PHP rest API to post data from JS client to a PHP 8 server.
I'm using the JS fetch() method. When using POST the formdata is send to PHP's global $_POST, but since I need to update data I have to use PUT. Somehow Google Chrome is not sending the formdata to the server and I don't know why. Somebody knows the reason?
JS:
const formData = new FormData();
formData.append('title', 'Hello world');
const options = {
method: 'PUT',
body: JSON.stringify(formData);
cache: 'no-cache',
mode: 'cors',
credentials: 'same-origin',
redirect: 'follow',
referrer: 'no-referrer',
// Not working either
// headers: {
// 'Content-Type': 'application/json; charset=UTF-8;'
// },
};
fetch('/some/api/v1', options);
PHP 8 server: var_dump($_REQUEST); // NULL
FormData. Just send that as body. Browser knows how to serialize it. See Upload File example in MDN Using Fetch