请求正文没有从 axios 传递给 Django Rest Framework [英] Request body is not being passed from axios get call to Django Rest Framework

查看:23
本文介绍了请求正文没有从 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框架中获取get请求数据

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

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