请求主体未从axios传递给Django Rest Framework的调用 [英] Request body is not being passed from axios get call to Django Rest Framework

查看:297
本文介绍了请求主体未从axios传递给Django Rest Framework的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在axios中发送带有正文的get请求时遇到问题。它不会将请求的主体传递给后端。

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代码如下所示

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);
            });
}

我正在使用Django作为后端,并且接收到空主体,即{},这会导致错误的请求发送到后端。我经历了几个堆栈溢出问题,但没有一个对我有帮助。

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.

更新

我的django代码看起来像如下所示

My django code looks like below

class TestView(APIView);
    def get(self, request):
       environment = request.data['environment']
       page_num = request.data['page_num'] 
       ...
       ...

在这里,当我无法获取环境或page_num数据时。当我从邮递员发送带有正文中的get调用和内容的请求时,它接受并发送回响应。

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.

重新更新

我注意到我们必须使用request.query_params ['some_val'],以防我们在Axios的请求中传递正文,但request.query_params如果我们用邮递员的尸体发送请求,则['some_val']将不起作用。我不确定这是否是正常行为!

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!

推荐答案

任何遇到此问题的人都可以在下面的链接中找到答案。

Anyone who's facing the issue can find the answer below to the link.

可能重复的
如何在django rest框架中访问获取请求数据

这篇关于请求主体未从axios传递给Django Rest Framework的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆