Using ksqlDB REST API with Python

Hi,

I try to use ksqlDB API (Run a ksqlDB Statement - ksqlDB Documentation) to use python code to interact with KSQLdb without CLI or Confluent UI.
If I try the curl command:

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:

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

1 Like

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

'{}`

should be

{}

Just a guess…

Thank @hoji but it is not the problem. :frowning_face:

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 :smiley:

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:

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

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