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

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));
HTTPClient http;
http.addHeader(“Content-Type”, “text/plain”);
int httpCode = http.POST(JSONmessageBuffer);
String payload = http.getString();

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”:“”,“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

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

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.