How to create an Item per REST API?

restapi
items
create
Tags: #<Tag:0x00007f51f00c2940> #<Tag:0x00007f51f00c2800> #<Tag:0x00007f51f00c2440>

(Dana) #1

I’ve tried to create a new Item in my OH2 System with the REST API Documentation.

I used a PUT to /rest/items/myTestItem
with the Body:
{
“type”: “string”,
“name”: “myTestItem”,
“label”: “Tester”
}

But instead of an 201, which says that the Item was created, I get an 400. which means that the Item is null.

Whats wrong with my Code?


Add new item using REST
(Dana) #3

I found my failure. It has to be type String mit upper S slight_smile:


(Kiran Patil) #4

Hi,

Can you post your complete code ?

We did as below but getting CORS error in browser as “Failed to load http://192.168.0.113:8181/rest/items/light25: Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.”

Code:

$.ajax({
            url: 'http://192.168.0.113:8181/rest/items/light25',
            headers: {
            "Access-Control-Allow-Origin": "http://192.168.0.113", //"http://192.168.0.113", //http://foo.example
           "Access-Control-Allow-Methods": "*", //"POST, GET, OPTIONS",
           "Access-Control-Allow-Headers": "*" //"X-PINGOTHER, Content-Type"
	   
           //"Access-Control-Allow-Headers": "Content-Type",
	               },
            method: 'PUT',
            //Accept: 'application/json',
            data: {"type": "String","name": "Tueblight1"},
            contentType: 'application/json',
            dataType: 'json',
            success: function(data) {
                  alert('GET completed');
                  $("#resultsdiv").html(JSON.stringify(data));
            },
            error: function(data) { 
                  alert(JSON.stringify(data));
            } 
          });

(Kiran Patil) #5

Nevermind, we got it working as below.

$.ajax({

url: 'http://192.168.0.113:8181/rest/items/light013222',
method: 'PUT',
data: JSON.stringify({"type": "String","name": "light013222"}),
contentType: 'application/json',
dataType: 'json',
success: function(data) {
alert('PUT completed');
$("#resultsdiv").html(JSON.stringify(data));
},
error: function(data) {
alert(JSON.stringify(data));
}
});