Im trying to learn REST API and got stuck with decoding data from JS FormData using PUT for example
JS:
newF = async (e) => {
let formData = new FormData();
formData.append('key1', 'value1');
formData.append('key2', 'value2');
let response = await fetch(api, {
method: 'PUT',
body: formData
});
let result = await response.json();
console.log(result)
}
newF()
PHP:
if ($_SERVER['REQUEST_METHOD'] === 'PUT') {
$myEntireBody = file_get_contents('php://input');
echo json_encode($myEntireBody);
}
This is closest that I got but the data returned to me is in strange format:
-----------------------------42245388588346180811466041785
Content-Disposition: form-data; name="key1"
value1
-----------------------------42245388588346180811466041785
Content-Disposition: form-data; name="key2"
value2
-----------------------------42245388588346180811466041785--
I dont know how to get values out of this with keys, I tried decoding/encoding var damp
$_PUT
in numerous ways and cant get values out.
I tried to follow among others: HTTP protocol's PUT and DELETE and their usage in PHP and Why PHP couldn't get $_POST data from Js FormData fetch? with no luck.
I just want to know how to get values out of JS FormData
on PHP side and echo one off them back to JS response to confirm it.