HTTP 405 when trying to send Temperaturvalue with JSON from ESP8266 to openhab

  • Platform information:
    • Hardware: Raspberry
    • OS: openhabianpi
    • openHAB version: 2.4
  • Issue of the topic: HTTP 405 , trying to send / set Temperaturvalue with JSON

Hi,
I’am trying to send a Temperaturvalue with JSON and just change the “state” of the item. But I always get a “HTTP error 405 Method not allowed”

Here is the code for the ESP (only the interesting part):
StaticJsonBuffer<200> JSONbuffer;
JsonObject& JSONencoder = JSONbuffer.createObject();
JSONencoder[“state”] = “21”;
char JSONmessageBuffer[200];
JSONencoder.prettyPrintTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
Serial.println(JSONmessageBuffer);
HTTPClient http;
http.begin(“http://Openhabserver:Port/rest/items/Temperatur_1/state”);
http.addHeader(“Content-Type”, “text/plain”);
int httpCode = http.POST(JSONmessageBuffer);
String payload = http.getString();
Serial.println(httpCode);
Serial.println(payload);

Here is the console-output:
22:05:22.065 -> Waiting for connection
22:05:22.065 -> {
22:05:22.065 -> “state”: “21”
22:05:22.065 -> }
22:05:22.112 -> 405
22:05:22.112 -> {“error”:{“message”:“HTTP 405 Method Not Allowed”,“http-code”:405,“exception”:{“class”:“javax.ws.rs.NotAllowedException”,“message”:“HTTP 405 Method Not Allowed”,“localized-message”:“HTTP 405 Method Not Allowed”}}}

From my understanding it’s possible to change value over the REST-API. Is JSON the wrong starting point ?

thanks in advance

regards, Dirk

Why?
Just send “21”

Sorry, I forgot to mention that I want to send the values over Wifi.
The ESP01 makes a connection and then send the value via HTTP to the openhabserver.

regards Tick

That’s not a problem.
The REST api is documented here:

You can install the REST API docs by following:

You will see that the item/state endpoint does take a JSON as input. Only the raw state is needed.

I found the curl statement in the REST-API Documentation.

curl -X PUT --header “Content-Type: text/plain” --header “Accept: application/json” -d “78” “http:///rest/items/Temperatur_1/state”

I thought JSON always wants key/value pairs. So how do I send just the value as you suggested ?

regards, Dirk

https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=2ahUKEwiDqoTs97_gAhUyt3EKHc39BpYQwqsBMAN6BAgFEAc&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3Dnmzt-a-WtWw&usg=AOvVaw2Jbvw5sEsvghjHfLLvXfxu

The rest API for items/itemName/state accepts a text/plain mime/content type. The payload (body) can be anything but since you are POSTing to Temperature_1 which I believe is a number item, the item can only accept a number. You are POSTing a JSON string to it. Just send 21 as mentioned above.

Although I am not sure why the error is 405. It should be 400 (bad request) per HTTP standards.

I think it would be ideal for you to understand how HTTP methods/verbs work and how your data contracts must be followed.