Skip to content

REST API sometimes returns HTML instead of JSON - is this intentional? #185

@FarooqAlaulddin

Description

@FarooqAlaulddin

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions