Rest API cURL post

Hi there,

I have been learning openhab2 for a couple of weeks.
I am trying to update a number item in openhab2 with the rest API.
My item looks like this:

{
“link”: “http://“MyIP”:8080/rest/items/LivingRoom_Temperature”,
“state”: “2.5”,
“stateDescription”: {
“pattern”: “%.1f °C”,
“readOnly”: false,
“options”: []
},
“type”: “Number”,
“name”: “LivingRoom_Temperature”,
“label”: “The Temperature is”,
“category”: “temperature”,
“tags”: [],
“groupNames”: [
“gTemperature”,
“gLivingRoom”
]
}

With python had no issue to update the value.

url = 'http://“myip”:8080/rest/items/LivingRoom_Temperature’
payload = 20
r = requests.post(url, payload)

but with cURL it doesn’t work with the following code:

curl -X post -d "21.0" http://"myip":8080/rest/items/LivingRoom_Temperature

Any ideas what I am doing wrong.

Thanks for the help.

Hi @nodoso,

try to enable CORS by setting the following line in runtime.cfg

org.eclipse.smarthome.cors:enable = true 

Note: that setting might get lost during an upgrade to a new version

with kind regards,
Patrik

Hi Patrick,

Thanks for the reply.
I still have the issue after updating the file.
I forgot to post the error message I receive:

{
“error”: {
“message”: “HTTP 415 Unsupported Media Type”,
“http-code”: 415,
“exception”: {
“class”: “javax.ws.rs.NotSupportedException”,
“message”: “HTTP 415 Unsupported Media Type”,
“localized-message”: “HTTP 415 Unsupported Media Type”
}
}

It seems it doesn’t like the value type.
Do I need to add something particular in the cURL --date or simply pass the value?

Thanks

Sorry - I do not know, but what Item type do you have … 1st I would try to update a simple string item to see if this works. If updating a string works then you know that basically it functions & you face a format/conversion issue.

with kind regards,
Patrik

Thanks for the help.
I finally found a solution, performed a tcpdump and checked the difference between the curl and python command.

It seems curl was sending the data with urlencode.
To simply send raw data with cURL you need to specify the -H “Content-Type: text/plain” option in the command.

The full command looks like this if it can help somebody:

curl -H "Content-Type: text/plain"  -X POST -d "string"  http://"yourIP":8080/rest/items/test

This works great.