Hello, I am working on a cloud storage website project, with a DRF backend and a react frontend. My upload view keeps giving me a 403 error, with details of: CSRF Failed: Origin checking failed - http://127.0.0.1:8000/ does not match any trusted origins.
This is particularly strange as I have other views that do not give this error, including a get view that is fetched on the same react router page as the problematic view. Provided below is the fetch request, and view responsible for the error:
// Problematic Fetch Request
const uploadFile = async () => {
const file = fileRef.current.files[0]
const tags = tagRef.current
const formData = new FormData();
formData.append('file', file);
formData.append('tags', tags);
console.log(getCookie('csrftoken'))
let response;
try {
response = await fetch(`/storage/upload`, {
headers: {
'X-CSRFToken': getCookie('csrftoken'),
},
credentials: 'include',
method: 'POST',
body: formData
})
console.log(response.status)
} catch (error) {
console.error('Error uploading file:', error);
}
}
// Problematic View
class FileUpload(APIView):
permission_classes = [permissions.IsAuthenticated]
authentication_classes = [SessionAuthentication]
def post(self, request):
return Response(status=status.HTTP_200_OK)
Any help would be appreciated, thank you!