Hi,
I'm trying to set up DRF as a read-only API, and want to require that all URLs have either a .api or .json format extension.
I've sort of managed this so far by copying the DefaultRouter class and in its get_urls() method changing this call:
urls = format_suffix_patterns(urls)
to this:
urls = format_suffix_patterns(
urls, suffix_required=True, allowed=["api", "json"]
)
This gets me what I need but leaves me with two remaining/resulting issues:
1. Tbe API Root URL looks like /api/v1/.api or /api/v1/.json. I feel it shouldn't have that final slash but, given all non-API URLs on the site have an ending slash, and I include my API URL conf with path("api/v1/", include("myproject.api.urls", namespace="v1")), I can't see how to fix it.
2. When I view the Browsable API, using the .api extension URLs, the links in the Breadcrumbs do not have the extension, and so clicking them 404s. I can't see any way to fix this other than subclassing BrowsableAPIRenderer and using a modified utils.breadcrumbs.get_breadcrumbs function.
I didn't think this was an unreasonable thing to want – explicit URLs for each format – but given the amount of fiddling required to get it functioning, maybe this is a dumb idea? Have I missed something? How do you set up your URLs and formats?
Many thanks,
Phil Gyford