# External function

**URL:** <https://www.singlestore.com/forum/t/external-function/3866>\
**Category:** Procedural SQL\
**Created:** [February 13, 2022, 5:04pm UTC](https://www.singlestore.com/forum/t/external-function/3866 "2022-02-13T17:04:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ira](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ira](https://www.singlestore.com/forum/u/ira)\
**Post date:** [February 13, 2022, 5:04pm UTC](https://www.singlestore.com/forum/t/external-function/3866/1 "2022-02-13T17:04:39Z")

</div>

Hi Guys,  
I created microservice to select data from PostgreSQL db. This microservice receives SQL query as input parameter with name sqlquery and returns json.  
It works e.g. see:

> curl --location --request POST “[http://localhost:3003/dbquery](http://localhost:3003/dbquery)” --data-urlencode “sqlquery=SELECT \* FROM public.players WHERE id \< 3”  
> Result: “[{"id":1,"name":"Petya"},{"id":2,"name":"Stepa"}]”

Then I created external functon (with necessary previous settings):  
SET GLOBAL enable\_external\_functions = ON;  
SET GLOBAL external\_functions\_allowlist = ‘{“endpoints” : [“[http://host.docker.internal:3003/dbquery](http://host.docker.internal:3003/dbquery)”]}’

CREATE OR REPLACE EXTERNAL FUNCTION get\_external\_data(sqlquery LONGTEXT)  
RETURNS LONGTEXT  
AS REMOTE SERVICE ‘[http://host.docker.internal:3003/dbquery](http://host.docker.internal:3003/dbquery)’  
FORMAT JSON;

SELECT get\_external\_data( ‘SELECT id, name FROM public.players’) AS res;

But there I see Error Code: 2601. External function runtime failure: Malformed JSON at offset 0

What should I add/change ?

(Apropos, I do it to fix missed feature - foreign/external/linked servers; it this feature planned already ?)

Thanks!

---

<div class="post-metadata">

**Author:** ![ira](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ira](https://www.singlestore.com/forum/u/ira)\
**Post date:** [February 14, 2022, 1:05pm UTC](https://www.singlestore.com/forum/t/external-function/3866/2 "2022-02-14T13:05:10Z")

</div>

@ [schristian](https://www.singlestore.com/forum/u/schristian),  
can you advice ?

---

<div class="post-metadata">

**Author:** ![schristian](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/s/41988e/32.png) [@schristian](https://www.singlestore.com/forum/u/schristian)\
**Post date:** [February 22, 2022, 7:12pm UTC](https://www.singlestore.com/forum/t/external-function/3866/3 "2022-02-22T19:12:42Z")

</div>

The json you return needs to be an object with the “data” tag.  
The json within the “data” object is an array of arrays with no tags, like this:

{“data”: [  
[0,“Petya”],  
[0,“Stepa”]  
]}

Also note that since only 1 request was sent to your service, all the rows you return should be for the same id sent to you (which would be 0).

---

<div class="post-metadata">

**Author:** ![ira](https://www.singlestore.com/forum/letter_avatar_proxy/v4/letter/i/9fc348/32.png) [@ira](https://www.singlestore.com/forum/u/ira)\
**Post date:** [February 23, 2022, 1:34pm UTC](https://www.singlestore.com/forum/t/external-function/3866/4 "2022-02-23T13:34:45Z")

</div>

@ [schristian](https://www.singlestore.com/forum/u/schristian), thank you.  
So, on now it’s impossible to use own and external post requests there and for same functionality it’s necessary to create very specific…  
Plan Singlestore change it ?
