问题
I have an issue while sending get request with a body in axios. It does not pass the body of the request to the backend.
Axios code looks like below
const FunctionName = (environment, page_num) => {
axios.get(API_URL,
{ params:
{
environment,
page_num
},
}).then(res => {
console.log(res);
}).catch(err => {
console.log(err.response.data);
});
}
I'm using Django as my backend and I'm receiving empty body i.e {} which causes bad request sent to the backend. I went through several stack overflow questions but none of them helped me. Can anyone please help me with this.
Update
My django code looks like below
class TestView(APIView);
def get(self, request):
environment = request.data['environment']
page_num = request.data['page_num']
...
...
Here when I'm unable to get the environment or page_num data. The same request when I send from postman with the get call and content in the request of the body, it accepts and sends the response back.
Re-Update
I noticed that we have to use request.query_params['some_val'] incase we're passing the body in a request from Axios but request.query_params['some_val'] will not work if we send a request with the body in postman. I'm not sure it is normal behavior or not!
回答1:
I'm not sure but try this:
axios({
method: "get",
url: API_URL,
body: {
environment,
page_num
}
}).then(res => console.log(res.data));
In Django, are you try get body with request.body
回答2:
Anyone who's facing the issue can find the answer below to the link.
Possible duplicate How to access get request data in django rest framework
来源:https://stackoverflow.com/questions/58184676/request-body-is-not-being-passed-from-axios-get-call-to-django-rest-framework