question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Pages detail API should allow fetching a page by path

See original GitHub issue

Currently the API endpoints provided by wagtail.api.v2.endpoints.PagesAPIEndpoint do not provide the ability to fetch a Page by the page’s URL.

The docs regarding filtering pages makes it seem like a request such as

curl http://mysite.com/api/pages/?html_url=<some_url>

should work but instead the API returns:

{
    "message": "query parameter is not an operation or a recognised field: html_url"
}

Would it be possible to support filtering on this field? Perhaps also making the detail view able to return the page by it’s path? i.e. allowing all of:

# filtering
curl http://mysite.com/api/pages/?html_url=<my_page_url>

# detail
curl http://mysite.com/api/pages/1
curl http://mysite.com/api/pages/my_page_path

If this is something that wagtail would like to add then I may be able to try submitting a PR.

The motivation for this feature is to make it easy for a frontend webapp to fetch pages and render them. It would be useful for SPA apps if they could integrate with a wagtail CMS backend by using the current pages URL to do a wagtail API request and use the JSON response to render a page using whatever framework the SPA is written with (react/angular/vue etc.).

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:5
  • Comments:5

github_iconTop GitHub Comments

1reaction
Place1commented, Jan 14, 2018

It’s awesome to see this issue has had some attention. I just had a look at #4195 and i’m wondering if that view returning a redirect will result in unnecessary round trips for API clients who just want to GET a page by it’s path. I know for my usecase it likely will. Would a permanent redirect help? Could the view just use Django’s reverse() to skip the redirect alltogether? @kaedroho

1reaction
Place1commented, Dec 22, 2017

After diving into the code a bit (i’m not familiar with it) I realize that achieving this through filtering probably isn’t feasible/practical for the API, but allowing individual pages to be retrieved by their path is possible.

i’ve hacked a little bit locally and this has some of my ideas in it:

class PagesAPIEndpoint(BaseAPIEndpoint):

    # ...

    def get_object(self):
        lookup = self.kwargs[self.lookup_field]
        if not re.match('^\d+$', lookup):
            return self.get_page_at_path(lookup)
        base = super(PagesAPIEndpoint, self).get_object()
        return base.specific

    def get_page_at_path(self, path):
        path_components = [component for component in path.split('/') if component]
        try:
            return self.request.site.root_page.specific.route(self.request, path_components).page.specific
        except Http404:
            raise Http404('No %s matches the given query.' % self.model._meta.object_name)

This would allow API clients to do the following:

curl http://mysite/api/pages/my/full/page/path

but there is no way to fetch the root page

curl http://mysite/api/pages/  # <-- this is the root path but also the List view :(

So this isn’t the solution but hopefully others have some better ideas?

Read more comments on GitHub >

github_iconTop Results From Across the Web

WP Rest API get page from path - WordPress Stack Exchange
I was thinking to setup a custom endpoint to take the path, run it through the get_page_by_path() function then return the id. But...
Read more >
Fetching data from the server - Learn web development | MDN
This article shows how to start working with Fetch to fetch data from the server.
Read more >
get_page_by_path() | Function
Retrieves a page given its path.
Read more >
Best practices for REST API design - Stack Overflow Blog
Learn how to design REST APIs to be easy to understand for anyone, future-proof, secure, and fast since they serve data to clients...
Read more >
RESTful web API design - Best Practices - Microsoft Learn
This mechanism is described in more detail in the section Use HATEOAS to enable navigation to related resources. In more complex systems, it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found