Postgrest doesn't like you executing arbitrary queries, but you can get around it by defining a function that executes the query for you:
$ psql mydb
mydb=# create function custom_query(query text) returns setof json as $f$
begin
return query execute format('with tmp as (%s) select row_to_json(tmp.*) from tmp;', query);
end
$f$ language plpgsql;