Django REST框架解析request.data引发错误

时间:2017-10-04 07:12:25

标签: python django django-rest-framework

在Django视图中,我正在尝试这个 -

    {
    "detail": "JSON parse error - No JSON object could be decoded"
}

在邮递员发布数据时,会引发属性错误

data = JSONParser().parse(request.data)

使用data = JSONParser().parse(request)代替AttributeError: 'QueryDict' object has no attribute 'read' 时收到此错误:

#include "ros/ros.h"

#include <image_transport/image_transport.h>

#include <cv_bridge/cv_bridge.h>

#include <sensor_msgs/image_encodings.h>

#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

#include "opencv2/core/version.hpp"
#include "opencv2/core/core.hpp"
#include <string>
#include <sstream>
#include <iostream>


#include <stdlib.h>
#include <stdio.h>

cv::Mat src_gray;
cv::Mat src;

cv::Vec3f circ;

cv::Mat blured;


namespace enc = sensor_msgs::image_encodings;

static const char WINDOW[] = "Image Processed";


image_transport::Publisher pub;


//This function is called everytime a new image is published
void imageCallback(const sensor_msgs::ImageConstPtr& original_image)
{
    //Convert from the ROS image message to a CvImage suitable for working with OpenCV for processing
    cv_bridge::CvImagePtr cv_ptr;
    try
    {
        cv_ptr = cv_bridge::toCvCopy(original_image, enc::BGR8);

        // copy pointer to mat:
        src= cv_ptr->image;
    }
    catch (cv_bridge::Exception& e)
    {
        //if there is an error during conversion, display it
        ROS_ERROR("tutorialROSOpenCV::main.cpp::cv_bridge exception: %s", e.what());
        return;
    }

    cv::medianBlur(src,blured,27);
    cv::cvtColor( blured, blured, CV_RGB2GRAY );
   cv::HoughCircles(blured,circ,1,20,50,30,30,0);





    //Display the image using OpenCV
    cv::imshow(WINDOW, blured);
    cv::waitKey(3);
        pub.publish(cv_ptr->toImageMsg());
}

1 个答案:

答案 0 :(得分:1)

当您使用drf时,请求是rest_framework.request.Request requests的实例,但对于rest_framework.parsers JSONParser.parser,您需要发送参数:django.core.handlers.wsgi.WSGIRequest,其中您可以通过request._request获得,但我认为只有你需要的是:

 serializer = PersonalInfoSerializer(data=request.data)