# REST API, help needed

**URL:** <https://community.dremio.com/t/rest-api-help-needed/10461>\
**Category:** Uncategorized\
**Created:** [March 29, 2023, 8:31pm UTC](https://community.dremio.com/t/rest-api-help-needed/10461 "2023-03-29T20:31:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![umar](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@umar](https://community.dremio.com/u/umar)\
**Post date:** [March 29, 2023, 8:31pm UTC](https://community.dremio.com/t/rest-api-help-needed/10461/1 "2023-03-29T20:31:53Z")

</div>

Can please someone explain this part of the code ?

**path = [‘"@elbert"’, ‘wta’, ‘matches’]**  
my username: omi  
DB: test-space  
Folder: nametesting  
table: abc\_testing.txt

> **[Dremio REST API](https://www.dremio.com/resources/tutorials/using-the-rest-api/)**
>
> The Dremio API is designed around RESTful principles, so next we will define some wrapper functions for HTTP GET, POST, PUT, and DELETE. Generally, POST and PUT requests will take parameters. For example, creating a source will take a source input....

def querySQL(query):  
queryResponse = apiPost(‘sql’, body={‘sql’: query})  
jobid = queryResponse[‘id’]  
return jobid

path = [‘"@elbert"’, ‘wta’, ‘matches’]  
path = ‘.’.join([str(x) for x in path])  
query = “SELECT \* FROM {source} WHERE winner\_name = ‘Serena Williams’ or loser\_name = ‘Serena Williams’”.format(source=path)  
jobid = querySQL(query)

---

<div class="post-metadata">

**Author:** ![Benny\_Chow](https://sea2.discourse-cdn.com/flex020/user_avatar/community.dremio.com/benny_chow/32/5527_2.png) [@Benny\_Chow](https://community.dremio.com/u/Benny_Chow)\
**Post date:** [April 2, 2023, 7:30pm UTC](https://community.dremio.com/t/rest-api-help-needed/10461/2 "2023-04-02T19:30:23Z")

</div>

It’s the fully qualified name of the table or view.  
@elbert = user’s home folder  
wta = sub-folder  
matches = table

---

<div class="post-metadata">

**Author:** ![umar](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@umar](https://community.dremio.com/u/umar)\
**Post date:** [April 3, 2023, 2:20pm UTC](https://community.dremio.com/t/rest-api-help-needed/10461/3 "2023-04-03T14:20:49Z")

</div>

Thank you for replying back. So I have been using this format but I get key error:id every single time when I run this part of the code. Any thoughts on that?

Also, @albert could be the bucket name?

---

<div class="post-metadata">

**Author:** ![balaji.ramaswamy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.dremio.com/balaji.ramaswamy/32/843_2.png) [@balaji.ramaswamy](https://community.dremio.com/u/balaji.ramaswamy)\
**Post date:** [April 8, 2023, 11:32pm UTC](https://community.dremio.com/t/rest-api-help-needed/10461/4 "2023-04-08T23:32:15Z")

</div>

@umar If you open the failed jobs, you will see download profile. Can you send the downloaded zip file and we can see the exact error

Thanks  
Bali

---

<div class="post-metadata">

**Author:** ![umar](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@umar](https://community.dremio.com/u/umar)\
**Post date:** [April 10, 2023, 3:31pm UTC](https://community.dremio.com/t/rest-api-help-needed/10461/5 "2023-04-10T15:31:22Z")

</div>

I don’t see download profile because I have not made it to that step yet. Below is the format of my code that I took from **[Dremio Tutorial: REST API Usage](https://www.dremio.com/resources/tutorials/using-the-rest-api/)** this website

import json  
import requests

username = ‘’  
password = ‘’  
headers = {‘content-type’:‘application/json’}  
dremioServer = ‘[https://myserver/apiv2](https://myserver/apiv2)’ # I took the port out

def apiGet(endpoint):  
return json.loads(requests.get(‘{server}/apiv2/{endpoint}’.format(server=dremioServer, endpoint=endpoint), headers=headers).text)

def apiPost(endpoint, body=None):  
text = requests.post(‘{server}/apiv2/{endpoint}’.format(server=dremioServer, endpoint=endpoint), headers=headers, data=json.dumps(body)).text

if (text):  
return json.loads(text)  
else:  
return None

def apiPut(endpoint, body=None):  
return requests.put(‘{server}/apiv2/{endpoint}’.format(server=dremioServer, endpoint=endpoint), headers=headers, data=json.dumps(body)).text

def apiDelete(endpoint):  
return requests.delete(‘{server}/apiv2/{endpoint}’.format(server=dremioServer, endpoint=endpoint), headers=headers)

def login(username, password):

# we login using the old api for now

loginData = {‘userName’: username, ‘password’: password}  
response = requests.post(‘[http://demo.drem.io:9047/apiv2/login](http://demo.drem.io:9047/apiv2/login)’, headers=headers, data=json.dumps(loginData))  
data = json.loads(response.text)

# retrieve the login token

token = data[‘token’]  
return {‘content-type’:‘application/json’, ‘authorization’:‘\_ dremio{authToken}’.format(authToken=token)}

headers = login(username, password)

**I used apiv2 instead of api/v3 becuase api/v3 was not working for me. Since logon is still using apiv2 so, I made the changes for rest of the code. Upto this point my code runs fine even if I use the port id or don’t. I do see my userid, name, email, token printed, but the job fails when I run the next part of the code shown below.**

def querySQL(query):  
queryResponse = apiPost(‘sql’, body={‘sql’: query})  
jobid = queryResponse[‘id’]  
return jobid

path = [‘"@myusername"’, ‘mainbucket’, ‘foldername’, ‘tablename’]  
path = ‘.’.join([str(x) for x in path])  
query = ‘’‘SELECT \* FROM “@myusername”.mainbucket.foldername.tablename’‘’.format(source=path)  
jobid = querySQL(query)

I get keyerror id by running the above piece of code. if i put port back here dremioServer = ‘[https://myserver/apiv2](https://myserver/apiv2)’, I get ('connection aborted., remote disconnected (‘remote end closed connection without response’)

How do I resolve this issue? Is this has to do with the code or the way I’m putting my sql code in there?  
I also tried different way of putting sql code where i took username out ( ‘’‘SELECT \* FROM mainbucket.foldername.tablename’‘’.format(source=path) ) but this did not work either.

---

<div class="post-metadata">

**Author:** ![balaji.ramaswamy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.dremio.com/balaji.ramaswamy/32/843_2.png) [@balaji.ramaswamy](https://community.dremio.com/u/balaji.ramaswamy)\
**Post date:** [April 11, 2023, 3:48pm UTC](https://community.dremio.com/t/rest-api-help-needed/10461/6 "2023-04-11T15:48:05Z")

</div>

@umar It does look like a formatting issue, can you first run the SQL on UI, make sure it works then grab that and use it via API?

---

<div class="post-metadata">

**Author:** ![umar](https://avatars.discourse-cdn.com/v4/letter/u/59ef9b/32.png) [@umar](https://community.dremio.com/u/umar)\
**Post date:** [April 11, 2023, 4:12pm UTC](https://community.dremio.com/t/rest-api-help-needed/10461/7 "2023-04-11T16:12:28Z")

</div>

**select \* from “mainbucket”.“foldername”.“tablename”** is the query that works for me in dremio UI.

Below is how I implement this query in rest api code  
**query = ‘’’ SELECT \* FROM “mainbucket”.“foldername”.“tablename” ‘’'.format(source=path)**

I’m still getting connection aborted error. Can you please assist me with the format I should be using in the code?

---

<div class="post-metadata">

**Author:** ![balaji.ramaswamy](https://sea2.discourse-cdn.com/flex020/user_avatar/community.dremio.com/balaji.ramaswamy/32/843_2.png) [@balaji.ramaswamy](https://community.dremio.com/u/balaji.ramaswamy)\
**Post date:** [April 12, 2023, 10:59pm UTC](https://community.dremio.com/t/rest-api-help-needed/10461/8 "2023-04-12T22:59:34Z")

</div>

@umar Are you using the below API?

[https://docs.dremio.com/software/rest-api/sql/](https://docs.dremio.com/software/rest-api/sql/)
