vervallsweg / cast-web-api

Quick and dirty web API for Google Cast enabled devices.
GNU General Public License v3.0
141 stars 74 forks source link

Possible format incompatibility with pycurl and requests modules #90

Closed Mr-jreeves closed 5 years ago

Mr-jreeves commented 5 years ago

Hello - I have been playing around with sending TTS to google home through cast-web-api (v1.0.2) using python on a raspberry pi ... after much debugging I found that the json data that cast-web-api is looking for must have the format [{ ... }] for the JSON. However, the json module does not add the brackets. I tried two methods: using pycurl and requests.

The following method using "requests module" will not work because the encoding is missing the [ ] (apparently it is a 'dict' object) and no way i know to force it.

from requests import post data = {"googleTTS":"en-US","mediaTitle":"test message to speak","mediaSubtitle": "","mediaImageUrl":""} response = post(DenHome_URL, json=data) print response.status_code

I was able to hack this code to work by adding the brackets ([]) around the data: c = pycurl.Curl() c.setopt(pycurl.URL, Home_URL) c.setopt(pycurl.HTTPHEADER, ['Content-Type:application/json']) data = json.dumps({"googleTTS":"en-US","mediaTitle":"test message to speak","mediaSubtitle": "","mediaImageUrl":""}) c.setopt(pycurl.POST, 1) c.setopt(pycurl.POSTFIELDS, '['+data+']') # <- note the hack adding the brackets. c.perform()

You can probably tell I am new to python so there might be something different I should be doing to get the json format correct.

Anyway, as of now, it works like a charm :-)

Mr-jreeves commented 5 years ago

well after more experimentation, I found the right way to use json.dumps and the post to make the requests module work. Key was to add the brackets "[]" directly in the dumps and not use the auto json encoding .. i.e. use "data" not "json" in the post.

d = json.dumps([{"googleTTS":"en-US","mediaTitle":"test message to speak","mediaSubtitle": "","mediaImageUrl":""}]) response = post(DenHome_URL, data=d)