Cannot convert a curl call to a RESTful command

I need to send an HTTP call from an automation. The curl command sent from within the HA container is below and it works

curl -XPOST https://api.nuki.io/smartlock/XXX/action -H 'authorization: Bearer 6XXXc' --json '{"action": 3}' -H 'content-type: application/json'

I sent the curl call from within the container to make sure everything works (connectivity, DNS, …)

I tried to convert this into an action in an automation:

alias: open Nuki via API
service: rest_command.axiom
data:
  url: https://api.nuki.io/smartlock/XXX/action
  method: POST
  headers:
    authorization: >-
      Bearer
      6XXXc
  payload: |
    {% set payload = {'action': 3} %}  {{ payload | to_json }}
  content_type: application/json

When running this action I get a “Action ran successfully” toaster but the command does not perform what it should.

The problem is that I have no way to debug it on the API side so before going into some crazy TLS proxification of the call to analyze it at the packet level I am hoping that there is an obvious error in my translation.

Notes:

  • I initially wrote authorization: Bearer 6XXXc in the headers section but due to the length of the token (I presume) the config was reformatted as per the above
  • there are no errors in the logs

EDIT: I realized I can use a simpler command, one that does not have a payload. Its translation is now:

url: https://api.nuki.io/smartlock/XXX/action/unlock
method: POST
headers:
  authorization: >-
    Bearer
    6XXXc

Now this does not work at all, an error pops up with TypeError: Type is not JSON serializable: LoggingUndefined

You appear to be trying to do the rest command configuration in the automation.

You need to define a rest command called rest_command.axiom:

Then call that in your automation.

I am not sure I understand - this is what I am doing (or think I am doing)

You can’t define rest commands in automation actions.

You need to define the restful command in the rest command integration. See the link I posted.

configuration.yaml ← ### HERE ###

rest_command:
  axiom:
    url: "http://example.com/"
    method: POST
    headers:
      authorization: >
        Bearer
        6XXXc
    payload: >
      {% set payload = {'action': 3} %}  {{ payload | to_json }}
    content_type: application/json

Restart home assistant.

Then you can use this command in your automation like this:

action:
  - service: rest_command.axiom

That’s it. Nothing more in the automation. No configuring the rest command in the automation actions.

1 Like

AHHHHH! I understand now. Thank you!

I read the docs, of course, but thought that this was a generic definition of a service. Since I saw in my automation “RESTful Command: axiom” I religiously copied the information from the docs in the data section :roll_eyes:

Now it is clear, thank you again!

EDIT: as a side note I see that over the years there is less and less configuration done via configuration.yaml and I tend to forget that this is a place to consider as well when trying stuff.

1 Like