Skip to content

Request Parsers

In most cases, the default content type for a REST API is JSON, but if you need to handle other content types (such as YAML, XML, CSV) or use a faster JSON parser, Django Ninja provides a parser configuration.

api = NinjaAPI(parser=MyYamlParser())

To create your own parser, you need to extend the ninja.parser.Parser class and override the parse_body method.

Example YAML Parser

Let's create our custom YAML parser:

import yaml
from typing import List
from ninja import NinjaAPI
from ninja.parser import Parser


class MyYamlParser(Parser):
    def parse_body(self, request):
        return yaml.safe_load(request.body)


api = NinjaAPI(parser=MyYamlParser())


class Payload(Schema):
    ints: List[int]
    string: str
    f: float


@api.post('/yaml')
def operation(request, payload: Payload):
    return payload.dict()

If you now send YAML as the request body like this:

ints:
 - 0
 - 1
string: hello
f: 3.14

It will be parsed correctly, and you should get JSON output like this:

{
  "ints": [
    0,
    1
  ],
  "string": "hello",
  "f": 3.14
}

Example ORJSON Parser

orjson is a fast, accurate JSON library for Python. It is the fastest Python JSON library in benchmarks, and more accurate than the standard json library or other third-party libraries.

pip install orjson

The parser code:

import orjson
from ninja import NinjaAPI
from ninja.parser import Parser


class ORJSONParser(Parser):
    def parse_body(self, request):
        return orjson.loads(request.body)


api = NinjaAPI(parser=ORJSONParser())

Done

Continue to the next chapter! Filtering

Comments


本文总阅读量次