Restful sensor with form-data

Hi,
am trying to set up some Restful sensors.

Using a rest client with the URL “http://intra.home:8010/ajax/fetch_collection.php
and the following form-data

Rest Client

No authentication is required.

I get this data:

{
  "state": "success",
  "message": "successfully fetched collection",
  "timings": {
    "login": 0.64199495315551758,
    "load": 0.751802921295166,
    "login_perc": 85.393940431815111,
    "load_perc": 14.605932716708306
  },
  "count": 16,
  "data": [
    {
      "mac": "ce:63:2c:3d:13:88",
      "name": "Device1",
    },
    {
      "mac": "09:aa:3b:47:a8:2d",
      "name": "Device2",
    },
    {
      "mac": "40:c7:23:9a:59:16",
      "name": "Device3",
    }
	...
  ]
}

I set up following rest-sonsor:

  - platform: rest
    name: "Unifi Client"
    resource: http://intra.home:8010/ajax/fetch_collection.php
    method: POST
    headers:
      accept: multipart/form-data
    params:
      selected_collection_method: list_clients
      selected_site_id: default
      selected_output_method: json
    value_template: "{{ value_json['data[0]']['mac'] }}"
    json_attributes_path: "$.data[0]"

Since I get no data back, I enabled logging for rest and see the following in the log:

[homeassistant.components.rest.sensor] Data fetched from resource: {"state":"success","message":"successfully fetched collection","timings":{"login":0,"load":0,"login_perc":0,"load_perc":0},"count":0,"data":[]}

Can somebody explain me how to set up the rest sensor correctly?

Thanks!

Try: {{ value_json.data.0.mac }}
The attributes path does nothing more than be a path, if you want to add attributes then you should put them onderneath

doesn’t work. this ends with an error while verifying the configuration in Homeassistant Developer Tools.

I think that the problem is that there is no data from the request because there is something wrong with the form-data.

I just tried out your example in json viewer (yes, I did remove the …) and it is not properly formatted as json. If you switch on ha logging then you will see the same.

sorry…
I missed to remove “,” when I pasted the output above (I do not want to upload the whole output the request produce)
This is the correct json output:

{
  "state": "success",
  "message": "successfully fetched collection",
  "timings": {
    "login": 0.64199495315551758,
    "load": 0.751802921295166,
    "login_perc": 85.393940431815111,
    "load_perc": 14.605932716708306
  },
  "count": 3,
  "data": [
    {
      "mac": "ce:67:2a:34:13:88",
      "name": "Device1"
    },
    {
      "mac": "09:cc:3b:57:a9:2d",
      "name": "Device2"
    },
    {
      "mac": "45:29:73:9a:50:16",
      "name": "Device3"
    }
  ]
}

i turned on logging

logger:
  default: warning
  logs:
    homeassistant.components.rest: debug

and I see that only this is coming back:

2023-01-11 17:02:26.852 DEBUG (MainThread) [homeassistant.components.rest.sensor] Data fetched from resource: {"state":"success","message":"successfully fetched collection","timings":{"login":0,"load":0,"login_perc":0,"load_perc":0},"count":0,"data":[]}

without “data”…

Well yes and no…it shows the data that it could collect with the information your provided in the ‘resource’
As I cannot replicate this on my system…not having same source, I have no clue what should be done.
Did you try this via CURL to find out if all is fine?

I briefly tested this by putting your json in config/www/test.json

 - platform: rest
    name: testing2
    scan_interval: 36000
    resource: "http://192.168.1.20:8124/local/test.json"
    value_template: "{{ value_json.data.0.mac }}"
    json_attributes_path: $.data.0
    json_attributes:
      - mac

thanks for this vingerha!
I also could test it now with the whole output I get in a rest client program. That’s fine.

But the problem I still have is that I cannot send the form-data parameters in home assistant.
Rest Client
(screenshot from rest client program)

params is specifically for query string parameters as noted in the documentation. The fact that you put multipart/form-data in headers doesn’t change anything, HA will make those into query string parameters and send it.

What you’d have to do here is get the raw request body and put that in payload. As the doc says on that field the payload is usually json but that’s not required. It won’t look nice like what you’re showing in the screenshot above. You need to copy the raw text as the tool you’re using sends it to the service after it processes that table you’re showing.

Also it looks like you added the header accept: multipart/form-data which seems incorrect. The Accept header is used to tell the service which content-types you are support for the response. You don’t want the response to be multipart/form-data, you want it to be application/json. You should set the content-type header to multipart/form-data to tell the service what format the payload you’re sending is in.

following CURL command gives the complete response:

curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -d "selected_collection_method=list_clients&selected_site_id=default&selected_output_method=json" http://intra.home:8010/ajax/fetch_collection.php

I also tried to change the rest sensor to

    headers:
      accept: application/x-www-form-urlencoded

but it’s still not working…
how the payload needs to be formatted to get the response as I get with CURL?

I am not at the level for this particular issue. @CentralCommand seems to be more at ease.
Other option if all fails and (!) curl provides the output as you say…is to run a command_line sensor with the curl, pipe to a file and use the file as the rest sensor resource. This is for sure not ‘neat’ but my 2cts

thanks for your help vingerha.