REST API sometimes returns HTML instead of JSON - is this intentional?
Hey folks,
I'm running into inconsistency with plone.rest that's causing issues in my headless setup. Sometimes when I hit a non-existent API endpoint, I get a 404 JSON response, but other times I get HTML from the classic UI instead.
What's happening
Let's say I have a content item at /my-page and I make API calls to:
/my-page/nonexistent → Returns JSON 404
/my-page/edit → Returns HTML edit form
Looking at the code in traverse.py , I can see why this happens. When a REST service isn't found, there's this fallback:
if service is None:
# No service, fallback to regular view
view = queryMultiAdapter((self.context, request), name=name)
if view is not None:
return view
raise
So if there's a browser view with that name (like /edit, /sharing, etc.), it returns the HTML view instead of a 404.
Why is this a problem?
I'm using Plone purely as a headless CMS. My frontend expects JSON from all API calls, so when it suddenly gets HTML, things break. I have to add extra error handling to check content types, which feels wrong.
My questions
- Is this fallback behavior intentional? I'm trying to understand why plone.rest was designed this way. Was it:
- Right now I'm monkey-patching the traverser to remove the fallback, but that's not ideal for maintenance.
- Anyone else hit this case? Am I missing something obvious here?
- Thoughts?
Environment:
- Plone 6.1.1
- Headless CMS setup with HTMX
REST API sometimes returns HTML instead of JSON - is this intentional?
Hey folks,
I'm running into inconsistency with plone.rest that's causing issues in my headless setup. Sometimes when I hit a non-existent API endpoint, I get a 404 JSON response, but other times I get HTML from the classic UI instead.
What's happening
Let's say I have a content item at
/my-pageand I make API calls to:/my-page/nonexistent→ Returns JSON 404/my-page/edit→ Returns HTML edit formLooking at the code in traverse.py , I can see why this happens. When a REST service isn't found, there's this fallback:
So if there's a browser view with that name (like
/edit,/sharing, etc.), it returns the HTML view instead of a 404.Why is this a problem?
I'm using Plone purely as a headless CMS. My frontend expects JSON from all API calls, so when it suddenly gets HTML, things break. I have to add extra error handling to check content types, which feels wrong.
My questions
Environment: