DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Enhanced API Security: Fine-Grained Access Control Using OPA and Kong Gateway
  • The Generic Way To Convert Between Java and PostgreSQL Enums
  • How Hasura 2.0 Works: A Design and Engineering Look
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines

Trending

  • The Missing `bandit` for AI Agents: How I Built a Static Analyzer for Prompt Injection
  • Building a Spring AI Assistant With MCP Servers: A Step-by-Step Tutorial
  • Your AI Agent Tests Are Passing, But Your Agent Is Still Broken
  • Architecting Zero-Trust AI Agents: How to Handle Data Safely
  1. DZone
  2. Data Engineering
  3. Databases
  4. Advanced URL Rewriting With Apache APISIX

Advanced URL Rewriting With Apache APISIX

Learn about using the proxy-rewrite plugin with a path variable in this post. You can reuse the same technique with multiple variables.

By 
Nicolas Fränkel user avatar
Nicolas Fränkel
·
Jul. 24, 24 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
3.5K Views

Join the DZone community and get the full member experience.

Join For Free

I spoke at Swiss PgDay in Switzerland in late June. The talk was about how to create a no-code API with the famous PostgreSQL database, the related PostgREST, and Apache APISIX, of course. I already wrote about the idea in a previous post. However, I wanted to improve it, if only slightly.

PostgREST offers a powerful SELECT mechanism. To list all entities with a column equal to a value, you need the following URL:

Shell
 
curl /products?id=eq.1


  1. id is the column
  2. eq.1 corresponds to the WHERE clause

In this case, the generated query is SELECT * FROM products WHERE id=1.

The query syntax is powerful and allows you to express complex queries. However, as an API designer, I want to avoid exposing users to this complexity. For example, a regular API can manage entities by their ID, e.g., /products/1. In turn, you'd expect PostgREST to be able to do the same with primary keys. Unfortunately, it doesn't treat primary keys any differently than regular columns. Apache APISIX to the rescue.

One of APISIX's best features is to rewrite requests, i.e., exposing /products/1 and forwarding /products?id=eq.1 to PostgREST. Let's do it.

First, we need to capture the ID of the path parameter. For this, we need to replace the regular radix tree router with the radix tree with a parameter router.

YAML
 
apisix:
    router:
        http: radixtree_uri_with_parameter


The next step is to rewrite the URL. We use the proxy-rewrite plugin for this on a /products/:id route. Unfortunately, using the :id parameter above in the regular expression is impossible. We need to copy it to a place that is accessible. To do that, before the rewriting, we can leverage the serverless-pre-function. With the plugin, you can write Lua code directly. It's an excellent alternative to a full-fledged plugin for short, straightforward snippets.

Here's the configuration:

Shell
 
curl -i http://localhost:9180/apisix/admin/plugin_configs/1 -X PUT -d '
{
  "plugins": {
    "serverless-pre-function": {
      "phase": "rewrite",
      "functions" : [
        "return function(_, ctx)
          ctx.var.product_id = ctx.curr_req_matched.id;         #1
        end"
      ]
    },
    "proxy-rewrite": {
      "uri": "/products?id=eq.$product_id"                      #2
    }
  }
}'


  1. Copy the captured id variable to a place accessible to other plugins later on
  2. Use it!

Thanks to my colleague Zeping for pointing out the solution to me!

You can expose the /products/1 REST-friendly URL and let APISIX rewrite it for PostgREST.

Conclusion

I've described using the proxy-rewrite plugin with a path variable in this post. You can reuse the same technique with multiple variables. Keep also in mind that the serverless plugin is a hidden jewel; it can help you with small Lua snippets before moving to a full-fledged plugin.

To Go Further

  • PostgREST
  • PostgREST tables and views
  • APISIX serverless plugin
API Lua (programming language) PostgreSQL

Published at DZone with permission of Nicolas Fränkel. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Enhanced API Security: Fine-Grained Access Control Using OPA and Kong Gateway
  • The Generic Way To Convert Between Java and PostgreSQL Enums
  • How Hasura 2.0 Works: A Design and Engineering Look
  • Beyond Manual Annotation: Engineering Self-Correcting Pseudo-Labeling Pipelines

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook