Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse SmartHome » Trying to add a new item to a registry through API
Trying to add a new item to a registry through API [message #1781155] Fri, 02 February 2018 07:08 Go to next message
Bhavana  S is currently offline Bhavana SFriend
Messages: 4
Registered: January 2018
Junior Member
Hello

Using curl command I am able to create item on ubuntu but when I translate the same syntax to javascript and run on windows I am getting this error - "Response for preflight has invalid HTTP status code 404"

CURL COMMAND

curl -X PUT --header "Content-Type: application/json" --header "Accept: application/json" -d "{
\"type\": \"light24\",
\"name\": \"String\",
\"label\": \"String\",
\"category\": \"String\",
\"tags\": [
\"String\"
],
\"groupNames\": [
\"String\"
],
\"groupType\": \"String\",
\"function\": {
\"name\": \"String\",
\"params\": [
\"String\"
]
}
}" http://192.168.0.113:8181/rest/items/light24

I HAVE TRANSLATED THE ABOVE CURL COMMAND TO THE FOLLOWING JAVASCRIPT CODE

$.ajax({

url: 'http://192.168.0.113:8181/rest/items/light25',
type: 'PUT',
data: JSON.stringify({"type": "light25","name": "String"}),
contentType: 'application/json',
dataType: 'json',
success: function(data) {
alert('PUT completed');
$("#resultsdiv").html(JSON.stringify(data));
},
error: function(data) {
alert(JSON.stringify(data));
}
});
I have also tried adding header
Access-Control-Allow-Origin : "*",
Access-Control-Allow-Methods: GET, POST, PUT
Access-Control-Allow-Headers: Content-Type
then also I am not able to create the item and getting same kind of error

Am I doing any thing wrong in javascript part...or is there any other method to do this
Can someone help me out on this please.
Re: Trying to add a new item to a registry through API [message #1781167 is a reply to message #1781155] Fri, 02 February 2018 09:22 Go to previous messageGo to next message
Simon Kaufmann is currently offline Simon KaufmannFriend
Messages: 51
Registered: January 2011
Member
You've mixed up the "name" and "type" attributes. "type" should be "String" and "name" should reflect the name that you also have at the end of the URL.
Re: Trying to add a new item to a registry through API [message #1781168 is a reply to message #1781167] Fri, 02 February 2018 09:28 Go to previous messageGo to next message
Bhavana  S is currently offline Bhavana SFriend
Messages: 4
Registered: January 2018
Junior Member
Sorry by mistake I have sent that code snippet

I have changed name as light24 and type as String only
then also it is not working

Is there any mistake other than that....

Is there any other way to create items
Re: Trying to add a new item to a registry through API [message #1781181 is a reply to message #1781168] Fri, 02 February 2018 10:38 Go to previous messageGo to next message
Simon Kaufmann is currently offline Simon KaufmannFriend
Messages: 51
Registered: January 2011
Member
Honestly, I don't know. If I take your example above (with the fixed name/type attributes), it works like a charm:

curl  -v -X PUT --header "Content-Type: application/json" --header "Accept: application/json" -d "{
\"type\": \"String\",
\"name\": \"light24\",
\"label\": \"String\",
\"category\": \"String\",
\"tags\": [
\"String\"
],
\"groupNames\": [
\"String\"
],
\"groupType\": \"String\",
\"function\": {
\"name\": \"String\",
\"params\": [
\"String\"
]
}
}" http://localhost:8080/rest/items/light24
*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> PUT /rest/items/light24 HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Content-Type: application/json
> Accept: application/json
> Content-Length: 211
>
* upload completely sent off: 211 out of 211 bytes
< HTTP/1.1 201 Created
< Content-Type: application/json
< Content-Length: 178
< Server: Jetty(9.3.15.v20161220)
<
* Connection #0 to host localhost left intact
{"link":"http://localhost:8080/rest/items/light24","state":"NULL","type":"String","name":"light24","label":"String","category":"String","tags":["String"],"groupNames":["String"]}%


Therefore it must be something in your local setup. The error message that you pasted in your initial post ("Response for preflight has invalid HTTP status code 404") is unknown to me, Eclipse SmartHome doesn't produce such error messages. And the curl command works. So either you are talking to the wrong server/port, or you configured the tool you are using incorrectly.
Re: Trying to add a new item to a registry through API [message #1781183 is a reply to message #1781181] Fri, 02 February 2018 11:01 Go to previous message
Bhavana  S is currently offline Bhavana SFriend
Messages: 4
Registered: January 2018
Junior Member
Thank you so much for replying Simon,

Its working fine now.
Previous Topic:Firmware API
Next Topic:label taken from channel-type and not from overwritten channel definition
Goto Forum:
  


Current Time: Fri Jun 22 03:54:49 GMT 2018

Powered by FUDForum. Page generated in 0.01730 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top