Django parse error

I am trying to implement a simple GET/POST api via Django REST framework views.py class cuser(APIView): def post(self, request): stream = BytesIO(request.DATA) json = JSONParser().parse(s...

I am trying to implement a simple GET/POST api via Django REST framework

views.py

class cuser(APIView):
def post(self, request):
   stream  = BytesIO(request.DATA)
    json = JSONParser().parse(stream)
    return Response()

urls.py

from django.conf.urls import patterns, url
from app import views
urlpatterns = patterns('',

           url(r'^challenges/',views.getall.as_view() ),
           url(r'^cuser/' , views.cuser.as_view() ),
      )

I am trying to POST some json to /api/cuser/ (api is namespace in my project’s urls.py ) ,
the JSON

{
"username" : "abhishek",
"email" : "john@doe.com",
"password" : "secretpass"
}

I tried from both Browseable API page and httpie ( A python made tool similar to curl)

httpie command

http --json POST http://localhost:58601/api/cuser/ username=abhishek email=john@doe.com password=secretpass

but I am getting JSON parse error :

JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Whole Debug message using --verbose --debug

    POST /api/cuser/ HTTP/1.1

Content-Length: 75

Accept-Encoding: gzip, deflate

Host: localhost:55392

Accept: application/json

User-Agent: HTTPie/0.8.0

Connection: keep-alive

Content-Type: application/json; charset=utf-8



{"username": "abhishek", "email": "john@doe.com", "password": "aaezaakmi1"}

HTTP/1.0 400 BAD REQUEST

Date: Sat, 24 Jan 2015 09:40:03 GMT

Server: WSGIServer/0.1 Python/2.7.9

Vary: Accept, Cookie

Content-Type: application/json

Allow: POST, OPTIONS



{"detail":"JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)"}

exceptions.py

Exceptions… allow error handling to be organized cleanly in a central or high-level place within the program structure.

— Doug Hellmann, Python Exception Handling Techniques

Exception handling in REST framework views

REST framework’s views handle various exceptions, and deal with returning appropriate error responses.

The handled exceptions are:

  • Subclasses of APIException raised inside REST framework.
  • Django’s Http404 exception.
  • Django’s PermissionDenied exception.

In each case, REST framework will return a response with an appropriate status code and content-type. The body of the response will include any additional details regarding the nature of the error.

Most error responses will include a key detail in the body of the response.

For example, the following request:

DELETE http://api.example.com/foo/bar HTTP/1.1
Accept: application/json

Might receive an error response indicating that the DELETE method is not allowed on that resource:

HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 42

{"detail": "Method 'DELETE' not allowed."}

Validation errors are handled slightly differently, and will include the field names as the keys in the response. If the validation error was not specific to a particular field then it will use the «non_field_errors» key, or whatever string value has been set for the NON_FIELD_ERRORS_KEY setting.

An example validation error might look like this:

HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 94

{"amount": ["A valid integer is required."], "description": ["This field may not be blank."]}

Custom exception handling

You can implement custom exception handling by creating a handler function that converts exceptions raised in your API views into response objects. This allows you to control the style of error responses used by your API.

The function must take a pair of arguments, the first is the exception to be handled, and the second is a dictionary containing any extra context such as the view currently being handled. The exception handler function should either return a Response object, or return None if the exception cannot be handled. If the handler returns None then the exception will be re-raised and Django will return a standard HTTP 500 ‘server error’ response.

For example, you might want to ensure that all error responses include the HTTP status code in the body of the response, like so:

HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 62

{"status_code": 405, "detail": "Method 'DELETE' not allowed."}

In order to alter the style of the response, you could write the following custom exception handler:

from rest_framework.views import exception_handler

def custom_exception_handler(exc, context):
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)

    # Now add the HTTP status code to the response.
    if response is not None:
        response.data['status_code'] = response.status_code

    return response

The context argument is not used by the default handler, but can be useful if the exception handler needs further information such as the view currently being handled, which can be accessed as context['view'].

The exception handler must also be configured in your settings, using the EXCEPTION_HANDLER setting key. For example:

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}

If not specified, the 'EXCEPTION_HANDLER' setting defaults to the standard exception handler provided by REST framework:

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'rest_framework.views.exception_handler'
}

Note that the exception handler will only be called for responses generated by raised exceptions. It will not be used for any responses returned directly by the view, such as the HTTP_400_BAD_REQUEST responses that are returned by the generic views when serializer validation fails.


API Reference

APIException

Signature: APIException()

The base class for all exceptions raised inside an APIView class or @api_view.

To provide a custom exception, subclass APIException and set the .status_code, .default_detail, and default_code attributes on the class.

For example, if your API relies on a third party service that may sometimes be unreachable, you might want to implement an exception for the «503 Service Unavailable» HTTP response code. You could do this like so:

from rest_framework.exceptions import APIException

class ServiceUnavailable(APIException):
    status_code = 503
    default_detail = 'Service temporarily unavailable, try again later.'
    default_code = 'service_unavailable'

Inspecting API exceptions

There are a number of different properties available for inspecting the status
of an API exception. You can use these to build custom exception handling
for your project.

The available attributes and methods are:

  • .detail — Return the textual description of the error.
  • .get_codes() — Return the code identifier of the error.
  • .get_full_details() — Return both the textual description and the code identifier.

In most cases the error detail will be a simple item:

>>> print(exc.detail)
You do not have permission to perform this action.
>>> print(exc.get_codes())
permission_denied
>>> print(exc.get_full_details())
{'message':'You do not have permission to perform this action.','code':'permission_denied'}

In the case of validation errors the error detail will be either a list or
dictionary of items:

>>> print(exc.detail)
{"name":"This field is required.","age":"A valid integer is required."}
>>> print(exc.get_codes())
{"name":"required","age":"invalid"}
>>> print(exc.get_full_details())
{"name":{"message":"This field is required.","code":"required"},"age":{"message":"A valid integer is required.","code":"invalid"}}

ParseError

Signature: ParseError(detail=None, code=None)

Raised if the request contains malformed data when accessing request.data.

By default this exception results in a response with the HTTP status code «400 Bad Request».

AuthenticationFailed

Signature: AuthenticationFailed(detail=None, code=None)

Raised when an incoming request includes incorrect authentication.

By default this exception results in a response with the HTTP status code «401 Unauthenticated», but it may also result in a «403 Forbidden» response, depending on the authentication scheme in use. See the authentication documentation for more details.

NotAuthenticated

Signature: NotAuthenticated(detail=None, code=None)

Raised when an unauthenticated request fails the permission checks.

By default this exception results in a response with the HTTP status code «401 Unauthenticated», but it may also result in a «403 Forbidden» response, depending on the authentication scheme in use. See the authentication documentation for more details.

PermissionDenied

Signature: PermissionDenied(detail=None, code=None)

Raised when an authenticated request fails the permission checks.

By default this exception results in a response with the HTTP status code «403 Forbidden».

NotFound

Signature: NotFound(detail=None, code=None)

Raised when a resource does not exists at the given URL. This exception is equivalent to the standard Http404 Django exception.

By default this exception results in a response with the HTTP status code «404 Not Found».

MethodNotAllowed

Signature: MethodNotAllowed(method, detail=None, code=None)

Raised when an incoming request occurs that does not map to a handler method on the view.

By default this exception results in a response with the HTTP status code «405 Method Not Allowed».

NotAcceptable

Signature: NotAcceptable(detail=None, code=None)

Raised when an incoming request occurs with an Accept header that cannot be satisfied by any of the available renderers.

By default this exception results in a response with the HTTP status code «406 Not Acceptable».

Signature: UnsupportedMediaType(media_type, detail=None, code=None)

Raised if there are no parsers that can handle the content type of the request data when accessing request.data.

By default this exception results in a response with the HTTP status code «415 Unsupported Media Type».

Throttled

Signature: Throttled(wait=None, detail=None, code=None)

Raised when an incoming request fails the throttling checks.

By default this exception results in a response with the HTTP status code «429 Too Many Requests».

ValidationError

Signature: ValidationError(detail, code=None)

The ValidationError exception is slightly different from the other APIException classes:

  • The detail argument is mandatory, not optional.
  • The detail argument may be a list or dictionary of error details, and may also be a nested data structure. By using a dictionary, you can specify field-level errors while performing object-level validation in the validate() method of a serializer. For example. raise serializers.ValidationError({'name': 'Please enter a valid name.'})
  • By convention you should import the serializers module and use a fully qualified ValidationError style, in order to differentiate it from Django’s built-in validation error. For example. raise serializers.ValidationError('This field must be an integer value.')

The ValidationError class should be used for serializer and field validation, and by validator classes. It is also raised when calling serializer.is_valid with the raise_exception keyword argument:

serializer.is_valid(raise_exception=True)

The generic views use the raise_exception=True flag, which means that you can override the style of validation error responses globally in your API. To do so, use a custom exception handler, as described above.

By default this exception results in a response with the HTTP status code «400 Bad Request».


Generic Error Views

Django REST Framework provides two error views suitable for providing generic JSON 500 Server Error and
400 Bad Request responses. (Django’s default error views provide HTML responses, which may not be appropriate for an
API-only application.)

Use these as per Django’s Customizing error views documentation.

rest_framework.exceptions.server_error

Returns a response with status code 500 and application/json content type.

Set as handler500:

handler500 = 'rest_framework.exceptions.server_error'

rest_framework.exceptions.bad_request

Returns a response with status code 400 and application/json content type.

Set as handler400:

handler400 = 'rest_framework.exceptions.bad_request'

Third party packages

The following third-party packages are also available.

DRF Standardized Errors

The drf-standardized-errors package provides an exception handler that generates the same format for all 4xx and 5xx responses. It is a drop-in replacement for the default exception handler and allows customizing the error response format without rewriting the whole exception handler. The standardized error response format is easier to document and easier to handle by API consumers.

Django Exceptions¶

Django raises some of its own exceptions as well as standard Python exceptions.

Django Core Exceptions¶

Django core exception classes are defined in django.core.exceptions.

AppRegistryNotReady

exception AppRegistryNotReady[source]

This exception is raised when attempting to use models before the app
loading process
, which initializes the ORM, is
complete.

EmptyResultSet

exception EmptyResultSet[source]

EmptyResultSet may be raised during query generation if a query won’t
return any results. Most Django projects won’t encounter this exception,
but it might be useful for implementing custom lookups and expressions.

FieldDoesNotExist

exception FieldDoesNotExist[source]

The FieldDoesNotExist exception is raised by a model’s
_meta.get_field() method when the requested field does not exist on the
model or on the model’s parents.

SuspiciousOperation

exception SuspiciousOperation[source]

The SuspiciousOperation exception is raised when a user has
performed an operation that should be considered suspicious from a security
perspective, such as tampering with a session cookie. Subclasses of
SuspiciousOperation include:

  • DisallowedHost
  • DisallowedModelAdminLookup
  • DisallowedModelAdminToField
  • DisallowedRedirect
  • InvalidSessionKey
  • RequestDataTooBig
  • SuspiciousFileOperation
  • SuspiciousMultipartForm
  • SuspiciousSession
  • TooManyFieldsSent

If a SuspiciousOperation exception reaches the ASGI/WSGI handler level
it is logged at the Error level and results in
a HttpResponseBadRequest. See the logging
documentation
for more information.

PermissionDenied

exception PermissionDenied[source]

The PermissionDenied exception is raised when a user does not have
permission to perform the action requested.

ViewDoesNotExist

exception ViewDoesNotExist[source]

The ViewDoesNotExist exception is raised by
django.urls when a requested view does not exist.

MiddlewareNotUsed

exception MiddlewareNotUsed[source]

The MiddlewareNotUsed exception is raised when a middleware is not
used in the server configuration.

ImproperlyConfigured

exception ImproperlyConfigured[source]

The ImproperlyConfigured exception is raised when Django is
somehow improperly configured – for example, if a value in settings.py
is incorrect or unparseable.

FieldError

exception FieldError[source]

The FieldError exception is raised when there is a problem with a
model field. This can happen for several reasons:

  • A field in a model clashes with a field of the same name from an
    abstract base class
  • An infinite loop is caused by ordering
  • A keyword cannot be parsed from the filter parameters
  • A field cannot be determined from a keyword in the query
    parameters
  • A join is not permitted on the specified field
  • A field name is invalid
  • A query contains invalid order_by arguments

ValidationError

exception ValidationError[source]

The ValidationError exception is raised when data fails form or
model field validation. For more information about validation, see
Form and Field Validation,
Model Field Validation and the
Validator Reference.

NON_FIELD_ERRORS

NON_FIELD_ERRORS

ValidationErrors that don’t belong to a particular field in a form
or model are classified as NON_FIELD_ERRORS. This constant is used
as a key in dictionaries that otherwise map fields to their respective
list of errors.

BadRequest

exception BadRequest[source]

The BadRequest exception is raised when the request cannot be
processed due to a client error. If a BadRequest exception reaches the
ASGI/WSGI handler level it results in a
HttpResponseBadRequest.

RequestAborted

exception RequestAborted[source]

The RequestAborted exception is raised when an HTTP body being read
in by the handler is cut off midstream and the client connection closes,
or when the client does not send data and hits a timeout where the server
closes the connection.

It is internal to the HTTP handler modules and you are unlikely to see
it elsewhere. If you are modifying HTTP handling code, you should raise
this when you encounter an aborted request to make sure the socket is
closed cleanly.

SynchronousOnlyOperation

exception SynchronousOnlyOperation[source]

The SynchronousOnlyOperation exception is raised when code that
is only allowed in synchronous Python code is called from an asynchronous
context (a thread with a running asynchronous event loop). These parts of
Django are generally heavily reliant on thread-safety to function and don’t
work correctly under coroutines sharing the same thread.

If you are trying to call code that is synchronous-only from an
asynchronous thread, then create a synchronous thread and call it in that.
You can accomplish this is with asgiref.sync.sync_to_async().

URL Resolver exceptions¶

URL Resolver exceptions are defined in django.urls.

Resolver404

exception Resolver404

The Resolver404 exception is raised by
resolve() if the path passed to resolve() doesn’t
map to a view. It’s a subclass of django.http.Http404.

NoReverseMatch

exception NoReverseMatch

The NoReverseMatch exception is raised by django.urls when a
matching URL in your URLconf cannot be identified based on the parameters
supplied.

Database Exceptions¶

Database exceptions may be imported from django.db.

Django wraps the standard database exceptions so that your Django code has a
guaranteed common implementation of these classes.

exception Error[source]
exception InterfaceError[source]
exception DatabaseError[source]
exception DataError[source]
exception OperationalError[source]
exception IntegrityError[source]
exception InternalError[source]
exception ProgrammingError[source]
exception NotSupportedError[source]

The Django wrappers for database exceptions behave exactly the same as
the underlying database exceptions. See PEP 249, the Python Database API
Specification v2.0, for further information.

As per PEP 3134, a __cause__ attribute is set with the original
(underlying) database exception, allowing access to any additional
information provided.

exception models.ProtectedError

Raised to prevent deletion of referenced objects when using
django.db.models.PROTECT. models.ProtectedError is a subclass
of IntegrityError.

exception models.RestrictedError

Raised to prevent deletion of referenced objects when using
django.db.models.RESTRICT. models.RestrictedError is a subclass
of IntegrityError.

HTTP Exceptions¶

HTTP exceptions may be imported from django.http.

UnreadablePostError

exception UnreadablePostError

UnreadablePostError is raised when a user cancels an upload.

Sessions Exceptions¶

Sessions exceptions are defined in django.contrib.sessions.exceptions.

SessionInterrupted

exception SessionInterrupted[source]

SessionInterrupted is raised when a session is destroyed in a
concurrent request. It’s a subclass of
BadRequest.

Transaction Exceptions¶

Transaction exceptions are defined in django.db.transaction.

TransactionManagementError

exception TransactionManagementError[source]

TransactionManagementError is raised for any and all problems
related to database transactions.

Testing Framework Exceptions¶

Exceptions provided by the django.test package.

RedirectCycleError

exception client.RedirectCycleError

RedirectCycleError is raised when the test client detects a
loop or an overly long chain of redirects.

Python Exceptions¶

Django raises built-in Python exceptions when appropriate as well. See the
Python documentation for further information on the Built-in Exceptions.

I’m working on a project that uses React as the front end that using DRF as the backend API. I’m testing a register form that happens use a CreateAPIView when it comes to deserializing data. There’s a problem however when JSON is accessed on the response object. The error is as follows:

{'detail': 'JSON parse error - Expecting property name enclosed in double quotes: line 1 column 2 (char 1)'}

I’m not sure what property name is being referred to since my test data is all in double quotes. I’m looking to understand what then would be causing this error?

class TestAPIRegisterView(TestCase):

    @classmethod
    def setUpTestData(cls):

        cls.register_form_data_2 = {
            "username": "User1",
            "password": "password",
            "password2": "passcode"
        }

    def test_register_user_password_match_fail(self):
        '''Verify that a 400 HTTP response is returned
        to the client informing it that the passwords entered
        in the register form don't match.'''

        http_response = self.client.post(
            reverse("register-user"),
            data=self.register_form_data_2,
            content_type="application/json"
        )

        print(http_response.json())
        self.assertEqual(http_response.status_code, 400)

**views.py**

class UserRegisterView(CreateAPIView):
    permission_classes = (permissions.AllowAny,)
    model = get_user_model()
    serializer_class = serializers.UserSerializer

**serializers.py**

class UserSerializer(serializers.ModelSerializer):
    password = serializers.CharField(write_only=True)
    password2 = serializers.CharField(write_only=True)

    def create(self, validated_data):
        user = get_user_model().objects.create(
            username=validated_data["username"],
        )
        user.set_password(validated_data['password'])
        user.save()
        return user

    def validate(self, data):
        password = data["password"]
        verify_password = data["password2"]

        if password != verify_password:
            raise serializers.ValidationError(
                "Cannot verify your password. Try again."
            )
        return data

    class Meta:
        model = get_user_model()
        fields = ["username", "password", "password2"]

Понравилась статья? Поделить с друзьями:
  • Django nginx 500 internal server error
  • Django models error messages
  • Django migrations error
  • Django login form error
  • Django logging error 500