Help with curl escape square brackets

Hey!

I´m using this curl command to send TTS notifications with a variable from my Mac Computer to Home Assistant:

curl -X POST -H "Authorization: Bearer MYTOKEN" -H "Content-Type: application/json" -d "{ \"entity_id\": \"media_player.schlafzimmer\", \"message\": \"test\" }" http://192.168.178.XX:8123/api/services/tts/google_say

and that works fine! Now I want to send TTS notifications from my mac to Alexa media Player, the service calls are a bit different, here is what I have so far:

curl -X POST -H "Authorization: Bearer MYTOKEN" -H "Content-Type: application/json" -d "{ \"target\": \"media_player.schlafzimmer_dot\", \"message\": \"test\", \"data\": \"type:tts\" }" http://192.168.178.XX:8123/api/services/notify/alexa_media_schlafzimmer_dot

But this is not working, I think its "data":{"type":"tts"}, I don´t know how to escape the second square brackets… Can anybody help?

I don’t see any square brackets. Probably lost due to markdown?

If you use the </> button (on the menu bar when writing a commend) around that code, it won’t remove any values from it and will keep all spacing.

oh sorry, I already tried to convert them. According to the Alexa Media Player configuration, the TTS looks like this:

{
"message":"test",
"data":{"type":"tts"},
"target":["group.alexa", "Guest Room", "media_player.kitchen", "serialNumber"]
}

so {"type":"tts"} is another curly bracket, and I don´t know how to escape that in the curl command

I tried:

-d "{ \"target\": \"media_player.schlafzimmer_dot\", \"message\": \"test\", \"data\": \"type:tts\" }"

but that is not working…

If you use single quotes, you shouldn’t have to escape any of the double quotes. So that can help a bit!

Also, I don’t think you need to escape the brackets.

And finally, you’re missing some quotes around these fields.

\"type:tts\" should be \"type\":\"tts\". Or, with brackets… \"data\":{\"type\":\"tts\"}

It’s really hard to find missing quotes when they are all escaped. I only notice after using single quotes :stuck_out_tongue:

So, give that a shot. With the single quotes…which I think works. If nothing else, adding the brackets and fixing the missing quotes should work.

curl -X POST -H 'Authorization: Bearer MYTOKEN' -H 'Content-Type: application/json' -d '{ "target":"media_player.schlafzimmer_dot", "message": "test", "data": {"type":"tts"} }' http://192.168.178.XX:8123/api/services/notify/alexa_media_schlafzimmer_dot
1 Like

Thank you Jim, got it working with brackets:

curl
-H “Accept: application/json”
-H “Authorization: Bearer TOKEN”
-X POST
-d “{ "target": "media_player.schlafzimmer_dot", "message": "$KMVAR_sound", "data":{"type":"tts"} }”
http://192.168.178.XX:8123/api/services/notify/alexa_media_schlafzimmer_dot

so now its possible to send variables with Keyboard Maestro on Mac to Home Assistant (In case someone is looking for this :slight_smile:

I would appreciate it if you could clarify the following for me. Your post shows two examples and neither of them match what jocnnor suggested.

  • Your first example uses double-quotes to delimit the entire string whereas jocnnor’s example suggested single-quotes (to differentiate from the double quotes used within the string).

  • Your second example (screenshot) also uses outer double-quotes and it also escapes the meaning of every double-quote within the string, which was not suggested in jocnnor’s example.

So do both examples you posted work?

oh sorry, I think I copied the wrong one! The Screenshot is the one that is working for me now. So I used jocnnors second suggestion:

Or, with brackets… \"data\":{\"type\":\"tts\"}

curl \
    -H "Accept: application/json" \
     -H "Authorization: Bearer TOKEN" \
    -X POST \
     -d "{ \"target\": \"media_player.schlafzimmer_dot\", \"message\": \"$KMVAR_sound\", \"data\":{\"type\":\"tts\"} }" \
     http://192.168.178.XX:8123/api/services/notify/alexa_media_schlafzimmer_dot

Can you try this and let me know if it works?

curl -X POST -H 'Authorization: Bearer MYTOKEN' -H 'Content-Type: application/json' --data-urlencode '{ "target":"media_player.schlafzimmer_dot", "message": "test", "data": {"type":"tts"} }' http://192.168.178.XX:8123/api/services/notify/alexa_media_schlafzimmer_dot

I’m curious to know if the --data-urlencode option works for this application.

its not working, thats what I get back:

Bildschirmfoto 2020-04-09 um 17.40.51

Thanks. In retrospect, that makes sense because Home Assistant does not perform an urldecode.