Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to add parser in router #1391

Open
bamjun opened this issue Jan 14, 2025 · 0 comments
Open

I want to add parser in router #1391

bamjun opened this issue Jan 14, 2025 · 0 comments

Comments

@bamjun
Copy link

bamjun commented Jan 14, 2025

I can create a custom parser when create the NinjaAPI but I can't create a custom parser in routers. How can I do this ?

problem


I want to add parser in router like blow.

from ninja import NinjaAPI
from ninja.parser import JSONParser, FormParser

api = NinjaAPI()

@api.post("/json/", parser=JSONParser())
def handle_json(request, data: dict):
    return {"received_data": data}

@api.post("/form/", parser=FormParser())
def handle_form(request, name: str, age: int):
    return {"name": name, "age": age}

I CAN DO


from ninja.parser import Parser
from lxml import etree

class XMLParser(Parser):
    def parse_body(self, request):
        body = request.body.decode()
        root = etree.fromstring(body)
        return {child.tag: child.text for child in root}

from ninja import NinjaAPI

api = NinjaAPI(parser=XMLParser())

@api.post("/xml/")
def handle_xml(request, data: dict):
    return {"received_data": data}

I WANT TO DO, BUT I CAN NOT


from ninja import NinjaAPI
from ninja.parser import JSONParser, FormParser

api = NinjaAPI()

@api.post("/json/", parser=JSONParser())
def handle_json(request, data: dict):
    return {"received_data": data}

@api.post("/form/", parser=FormParser())
def handle_form(request, name: str, age: int):
    return {"name": name, "age": age}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant