Trying to add a new item to a registry through API [message #1781155] |
Fri, 02 February 2018 07:08 |
Bhavana S Messages: 5 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 #1781181 is a reply to message #1781168] |
Fri, 02 February 2018 10:38 |
Simon Kaufmann 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.
|
|
|
|
Powered by
FUDForum. Page generated in 0.03764 seconds