Help with formatting REST API

I am using the HTTP REST API to call using POST calling services, I have been able to use it already successfully. But having problem with this YAML, It works using the developer panel of the HA. But when I try to convert it to POSTMAN or Python code, the response is 400. Can anybody help me with coding this to python.

service: media_player.play_media
target:
  entity_id: media_player.chromecast
data:
  media_content_id: ' { "app_name": "youtube", "media_id": "CKjSm5ZeehE" }'
  media_content_type: cast

I believe it is the “media_content_id” line that is tripping me up.

Thanks in advance

this is what I have now and variations of it

req = urequests.request(method=‘POST’, url=‘https://vvvvv.duckdns.org/api/services/media_play/play_media’,json=json.dumps(({‘entity_id’:‘media_player.chromecast’,‘media_content_type’:‘cast’,‘media_content_id’:({‘app_name’:‘youtube’,‘media_id’:'CKjSm5ZeehE’})})), headers={‘Authorization’:‘Bearer secret’,‘Content-Type’:‘application/json’})

Since I have not gotten any response, maybe my original post is not clear. So hopefully this is clearer

able to successfully get a response from my chromecast using the developer tool

But when I replicated using postman it fails, either code 400 or not valid JSON. FYI, I have successfully used postman for other HA REST calls.

so any advice?

try setting content type to application/json and also dump the python dict to a json object when setting the body.

from requests import post
import json

url = "https://hass.xyz.net/api/services/media_player/play_media"
headers = {
    "content-type": 'application/json',
    "Authorization": "Bearer ABCDE"}

data = {
    "entity_id": "media_player.office",
    "media_content_id": "'Turn lights red'",
    "media_content_type": "custom"
}
response = post(url, headers=headers, data=json.dumps(data))
print(response)

This isn’t the same thing as this:

In developer tools the value of media_content_id is a string. In postman you are trying pass a JSON object as the value. Not the same thing and hence you’re getting an error.

Assuming your call in dev tools was successful the equivalent json to provide in postman would be this:

{
  "entity_id": "media_player.chromecast",
  "media_content_type": "cast",
  "media_content_id": "{\"app_name\": \"youtube\", \"media_id\": \"CKjSm5ZeehE\"}"
}