Querying postgrest with httpx

57 Views Asked by At

Consider the first "Operator Modifiers" example from https://postgrest.org/en/stable/references/api/tables_views.html#operator-modifiers:

curl "http://localhost:3000/people?last_name=like(any).{O*,P*}"

If I try to replicate it using httpx (or requests, urllib3 or any other Python module I tried) the URL that ends up at PostgREST's end is urlencoded:

>>> import https
>>> r = httpx.get("http://localhost:3000/people?last_name=like(any).{O*,P*}")
>>> print(r.url)
URL('http://localhost:3000/people?last_name=like(any).%7BO*,P*%7D')

PostgREST responds with an error to that:

{'code': '22P02',
 'details': 'Expected ":", but found "}".',
 'hint': None,
 'message': 'invalid input syntax for type json'}

It does not seem possible to disable the automatic urlencoding on the client's end.

How can I query PostgREST using httpx?

1

There are 1 best solutions below

1
larsks On

The issue here has nothing to do with URL encoding. URL encoding is required by the standard, and if postgrest actually had a problem with that it would be buggy.

The problem is that your curl command line doesn't do what you think it does.

curl treats a {a,b,c} expression a bit like the shell: it will expand it into multiple distinct URLs. So when you write:

curl "http://localhost:3000/people?last_name=like(any).{O*,P*}"

Your are effectively running multiple curl commands:

curl "http://localhost:3000/people?last_name=like(any).O*"
curl "http://localhost:3000/people?last_name=like(any).P*"

This is described in the "Globbing" section of the curl man page.

I think you will find that you can request either of the above URLs using httpx (etc) without a problem.


You can verify the above by running curl with the --trace-ascii log option, and then inspecting the resulting log file. You will see multiple GET requests.

You can disable globbing with the --globoff (-g) option.