admin管理员组文章数量:1316980
I have an endpoint that I made using djangorestframework. The view has a dispatch method for retrieving data from the request and sending it to post method. Endpoint:
class Profile(APIView):
permission_classes = [IsAuthenticated]
data = None
def dispatch(self, request, *args, **kwargs):
if request.content_type == 'application/json':
try:
self.data = json.loads(request.body)
except json.JSONDecodeError:
return super().dispatch(request, *args, **kwargs)
else:
return super().dispatch(request, *args, **kwargs)
I tested this endpoint with 'Authorization': 'Bearer ${token}'
header using postman and rest file, and I am getting the data back with status 200. But when I do the same thing in React using axios.post method, the same user is unauthorized.
Axios.post:
useEffect(() => {
axios.post('http://127.0.0.1:8000/user/profile/', {
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: {
"username": "Caution1",
"password": "Caution123"
}
}).then(response => {setData(response.data); console.log(data);});
}, []);
I have CORS_ALLOW_ALL_ORIGINS
and CORS_ALLOW_CREDENTIALS
both to True.
I have an endpoint that I made using djangorestframework. The view has a dispatch method for retrieving data from the request and sending it to post method. Endpoint:
class Profile(APIView):
permission_classes = [IsAuthenticated]
data = None
def dispatch(self, request, *args, **kwargs):
if request.content_type == 'application/json':
try:
self.data = json.loads(request.body)
except json.JSONDecodeError:
return super().dispatch(request, *args, **kwargs)
else:
return super().dispatch(request, *args, **kwargs)
I tested this endpoint with 'Authorization': 'Bearer ${token}'
header using postman and rest file, and I am getting the data back with status 200. But when I do the same thing in React using axios.post method, the same user is unauthorized.
Axios.post:
useEffect(() => {
axios.post('http://127.0.0.1:8000/user/profile/', {
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
body: {
"username": "Caution1",
"password": "Caution123"
}
}).then(response => {setData(response.data); console.log(data);});
}, []);
I have CORS_ALLOW_ALL_ORIGINS
and CORS_ALLOW_CREDENTIALS
both to True.
- Can you inspect network in your browser and check the response? It can provides userful informations: are the headers correctly set, response code, browser error (e.g. CORS) etc... – Jean Bouvattier Commented Feb 10 at 9:28
1 Answer
Reset to default 0rewrite your request in axios
useEffect(() => {
axios.post('http://127.0.0.1:8000/user/profile/', {
"username": "Caution1",
"password": "Caution123"
},{
headers: {
"Content-Type": "application/json",
"Authorization": `Bearer ${token}`
},
}).then(response => {setData(response.data); console.log(data);});
}, []);
本文标签: reactjsCan39t get access to an endpointStack Overflow
版权声明:本文标题:reactjs - Can't get access to an endpoint - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741983389a2408532.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论