2016-12-02 41 views
1

假設你有一個函數返回在你的postgresql數據庫中定義的json。openresty上游postgres調用一個函數,它自己返回一個json

CREATE OR REPLACE FUNCTION test() RETURNS JSON AS $$ 
    SELECT 
    '[ 
    {"fName":"John","lName":"Doe"}, 
    {"fName":"Jane","lName":"Doe"} 
    ]'::JSON; 
$$ 
LANGUAGE SQL STRICT IMMUTABLE; 


SELECT test(); 
------------------------------------- 
[         
    {"fName":"John","lName":"Doe"}, 
    {"fName":"Jane","lName":"Doe"} 
] 

此外你有Nginx的包括Postgres的Nginx的模塊(openresty) 與follwoing配置文件:

worker_processes 1; 
error_log logs/error.log; 
events { 
    worker_connections 1024; 
} 
http { 

    upstream database { 
     postgres_server localhost dbname=example user=postgres; 
     postgres_keepalive max=200 overflow=reject; 
    } 

    server { 
     listen 8080; 
     location /test/ { 
       postgres_pass database; 
       rds_json on; 
       postgres_query HEAD GET "SELECT test()"; 
       postgres_rewrite HEAD GET no_rows 410; 
     } 
    } 
} 

隨着rds_json上;所有報價都逃脫輸出,它看起來像這樣:

curl http://localhost:8080/test/ 
[{"test":"[\n {\"fName\":\"John\",\"lName\":\"Doe\"},\n {\"fName\":\"Jane\",\"lName\":\"Doe\"}\n ]"}] 

的,如果我設置rds_json關閉;我收到格式正確的JSON,但返回的字符串開始和結束與一些尷尬的跡象:

@^C^@^@^@^@^@^@^B^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^@^A^@^@<80>r^@^D^@test^AL^@^@^@[ 
    {"fName":"John","lName":"Doe"}, 
    {"fName":"Jane","lName":"Doe"} 
    ]^@ 

是否有任何擺脫的結果對象的附加標誌的方式或不逃避所有的雙引號在第一種情況下?

謝謝

回答

0

我找到了一個理由。其實這是微不足道的。而不是讓openresty格式輸出到json。對不起,我的問題困擾社區。 我必須正確設置標題:

worker_processes 1; 
error_log logs/error.log; 
events { 
    worker_connections 1024; 
} 
http { 

    upstream database { 
     postgres_server localhost dbname=example user=postgres; 
     postgres_keepalive max=200 overflow=reject; 
    } 

    server { 
     listen 8080; 
     location /test/ { 
       postgres_pass database; 
       postgres_query HEAD GET "SELECT test()"; 
       more_set_headers 'Content-Type: application/json'; 
       postgres_rewrite HEAD GET no_rows 410; 
     } 
    } 
} 
相關問題