I follow the example in https://scotch.io/tutorials/create-a-custom-usefetch-react-hook, to perform a fetch. All works.
I change it slightly to make a POST with 'Accept': 'application/json'
and 'Content-Type': 'application/json'
as per https://stackoverflow.com/a/29823632. However, no matter how I post, it is still of Text/Html context type.
const useFetch = (url, body) => {
const [response, setResponse] = React.useState(null);
const [error, setError] = React.useState(null);
React.useEffect(() => {
const FetchData = async () => {
try {
const method = "POST"
const headers = {
'Accept': 'application/json',
'Content-Type': 'application/json'
};
const options = {
method,
headers
};
if (body) options.body = JSON.stringify(body);
const res = await fetch(url, options);
const json = await res.json();
setResponse(json);
} catch (error) {
setError(error);
}
};
FetchData();
}, []);
return { response, error };
};
export default useFetch
What did I do wrong?