HTTP API from javascript

19 views
Skip to first unread message

Federico Sarfatti

unread,
Aug 29, 2023, 1:17:45 PM8/29/23
to Signal K
Hello everyone!

I'm trying to put data into signalK server via HTTP API REST.

I can login with following code:

const username="admin";
const password="1234";
const param=JSON.stringify({username,password});
fetch(this.url+"/signalk/v1/auth/login",{
    method:"POST",
    withCredentials:true,
    headers:{
        Accept":"application/json",
        Content-Type":"application/json"
    },
    body:param
})
.then(r => r.json())
.then(r => {
     console.log(JSON.stringify(r));
  })

I obtein the token:
{"token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6ImFkbWluIiwiaWF0IjoxNjkzMzI4NTk1LCJleHAiOjE2OTM0MTQ5OTV9.MBRfPN4tqDNnD7yp8lGhLqRoPTO7BJfYCk5gF2yIx4A"}

but when I put a request to send a new value to server I obtein a permission error:

const value="390";
const source="fridge";
const param=JSON.stringify({value,source});
fetch(this.url+"/signalk/v1/api/vessels/self/environment/inside/refrigerator/temperature",{
method:"PUT",
withCredentials:false,
headers:{
"Accept":"application/json",
"Content-Type":"application/json"
},
body:param
})
.then(r => r.json())
.then(r => {
       console.log(JSON.stringify(r));
})

ERROR:
{"error":"Permission Denied"}

Any idea to solve this?.. 

thanks in advance!
Reply all
Reply to author
Forward
0 new messages