# Using ksqlDB REST API with Python

**URL:** <https://forum.confluent.io/t/using-ksqldb-rest-api-with-python/2732>\
**Category:** ksqlDB\
**Created:** [14 September 2021 14:04 UTC](https://forum.confluent.io/t/using-ksqldb-rest-api-with-python/2732 "2021-09-14T14:04:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![GiuseppeR](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@GiuseppeR](https://forum.confluent.io/u/GiuseppeR)\
**Post date:** [14 September 2021 14:04 UTC](https://forum.confluent.io/t/using-ksqldb-rest-api-with-python/2732/1 "2021-09-14T14:04:51Z")

</div>

Hi,

I try to use ksqlDB API ([Run a ksqlDB Statement - ksqlDB Documentation](https://docs.ksqldb.io/en/latest/developer-guide/ksqldb-rest-api/ksql-endpoint/)) to use python code to interact with KSQLdb without CLI or Confluent UI.  
If I try the curl command:

```auto
curl --http1.1 -X "POST" "http://192.XX.XX.XX:8088/ksql" -H "Accept: application/vnd.ksql.v1+json" -H "Content-Type: application/json" -d $'{"ksql": "LIST STREAMS;","streamsProperties": {}}'

```

it works.  
If I try to translate in Python this call:

```auto
import requests

url = 'http://192.XX.XX.XX:8088/ksql'
headers = {'accept': 'application/vnd.ksql.v1+json', 'content-type': 'application/json' }
payload = {'ksql': 'LIST STREAMS;', 'streamsProperties': '{}'}

r = requests.post(url, headers=headers, data=payload)
print(r)

```

I receive a 400 status code.  
There is something wrong?  
Any help is apreciated.  
Giuseppe

---

<div class="post-metadata">

**Author:** ![hoji](https://avatars.discourse-cdn.com/v4/letter/h/cdc98d/32.png) [@hoji](https://forum.confluent.io/u/hoji)\
**Post date:** [14 September 2021 19:55 UTC](https://forum.confluent.io/t/using-ksqldb-rest-api-with-python/2732/2 "2021-09-14T19:55:33Z")

</div>

Could it be the streamsProperties dictionary?.. looks like you have it as a string:

```auto
'{}`

```

should be

```auto
{}

```

Just a guess…

---

<div class="post-metadata">

**Author:** ![GiuseppeR](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@GiuseppeR](https://forum.confluent.io/u/GiuseppeR)\
**Post date:** [15 September 2021 06:35 UTC](https://forum.confluent.io/t/using-ksqldb-rest-api-with-python/2732/3 "2021-09-15T06:35:38Z")

</div>

Thank @hoji but it is not the problem. ☹

---

<div class="post-metadata">

**Author:** ![rmoff](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.confluent.io/rmoff/32/3_2.png) [@rmoff](https://forum.confluent.io/u/rmoff)\
**Post date:** [15 September 2021 09:09 UTC](https://forum.confluent.io/t/using-ksqldb-rest-api-with-python/2732/4 "2021-09-15T09:09:14Z")

</div>

I think the problem is with the construction of the `payload` value

If you change this

> `payload = {'ksql': 'LIST STREAMS;', 'streamsProperties': '{}'}`

to this

> `payload = '{"ksql": "LIST STREAMS;","streamsProperties": {}}'`

Then the code runs successfully. I’m not a python expert so I can’t tell you if this is the _correct_ way to do it, but the call does succeed 😃

You might also want to use `print(r.text)` since you get a more verbose output if there is a problem with the call.

* * *

BTW if you’re using Python you might be interested in this project:

> **[GitHub - bryanyang0528/ksql-python: A python wrapper for the KSQL REST API.](https://github.com/bryanyang0528/ksql-python)**
>
> A python wrapper for the KSQL REST API. Contribute to bryanyang0528/ksql-python development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![GiuseppeR](https://avatars.discourse-cdn.com/v4/letter/g/d07c76/32.png) [@GiuseppeR](https://forum.confluent.io/u/GiuseppeR)\
**Post date:** [15 September 2021 09:56 UTC](https://forum.confluent.io/t/using-ksqldb-rest-api-with-python/2732/5 "2021-09-15T09:56:37Z")

</div>

Thank you @rmoff ! your help is very ok! Now it works!  
I’m trying the client suggested…  
Thnaks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/confluentcommunity/original/1X/c49438c90c9df282e9996fdf6971be890c71b65a.svg) [@system](https://forum.confluent.io/u/system)\
**Post date:** [22 September 2021 09:56 UTC](https://forum.confluent.io/t/using-ksqldb-rest-api-with-python/2732/6 "2021-09-22T09:56:54Z")

</div>

Closed on 22 September 2021 09:56 UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
