Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

json POST syntax


Question

Posted

Hi,

 

Im trying to control a Danalock through a Danabridge and so far i can get the token and use it to retrieve my locks and its status but when trying to perform lock/unlock i get a 404 error.

I have translated a python implementation but this last part is not working.

 

The encoded json from python looks like this:

payload = json.dumps({"device": serial, "operation": "afi.lock.operate", "arguments": [command]})

The command here is either "lock" or "unlock" but since its inside brackets im not sure how to translate it.

"arguments": ["lock"] and "arguments": "[lock]" doesnt work.

 

Does anyone have an idea how the syntax should be?

 

My POST in lua looks like this:

http:request('https://bridge.danalockservices.com/bridge/v1/execute',{
    options = {
      method = 'POST',
      headers = {
        ['Accept'] = 'application/json',
        ['Content-Type'] = 'application/json',
        ['Authorization'] = 'Bearer ' ..token          
      },
        data = json.encode({
        ["device"] = lockserial,
        ["operation"] = "afi.lock.operate",
        ["arguments"] = '["lock"]'
          })
  },

 

BR

 

Recommended Posts

  • 0
Posted

Try arguments as LUA array

 

["arguments"] = {"lock"}

  • 0
  • Inquirer
  • Posted

    Thanks ivx, but it seems i still get a 404..

     

    BR

    • 0
    Posted (edited)

    HTTP error 404 means URL(https://bridge.danalockservices.com/bridge/v1/execute) does not exist and seems not related to arguments values.

     

    Edited by ivx
    • 0
    Posted

    Just try to get locks list, can you see lock in list and serial is as you set.

     

     url = "https://api.danalock.com/locks/v1"
     r = requests.get(url, headers={'Authorization': 'Bearer ' + token, 'Accept': 'application/json', 'content-type': 'application/json'})

     

    Please login or register to see this link.

    • 0
  • Inquirer
  • Posted

    Yes, i can get both lock list and also its status (using

    Please login or register to see this link.

    But as soon as i try to lock it i get the 404 (And sometimes i have seen 500 also)

     

    BR

    • 0
    Posted

    Try to get state pf lock

     

    execute_url = "https://bridge.danalockservices.com/bridge/v1/execute"

        payload = json.dumps({"device": serial, "operation": "afi.lock.get-state"})

        r = requests.post(execute_url, headers={'Authorization': 'Bearer ' + token, 'Accept': 'application/json', 'content-type': 'application/json'}, data=payload)
     

    If it's arguments problem - .get-state doesn't have array arguments, so does this work ?

    • 0
  • Inquirer
  • Posted

    Yes, "afi.lock.get-state" works

    • 0
    Posted

    use print() out json.encode() and post here.

    • 0
  • Inquirer
  • Posted

    {"operation":"afi.lock.operate","arguments":["lock"],"device":"XX:XX:XX:XX:XX:XX"}

    • 0
    Posted (edited)

    Seems ok, but does remote server check arguments order - which is not same.

    Try pass raw string(without json.encode)

    '{"device":"XX:XX:XX:XX:XX:XX","operation":"afi.lock.operate","arguments":["lock"]}'

    Edited by ivx
    • 0
  • Inquirer
  • Posted

    Now i got a 500 instead.. (internal error)

    After some re-tests, i still get 404

    • 0
    Posted

    Now syntax is right, can't suggest more. Only you can try 

    Please login or register to see this link.

     , with this you can see raw 404 response, sometimes i have got more info from there.

     

    You can get token and print it out from current code. Just make 

    Please login or register to see this link.

     post.

    • 0
  • Inquirer
  • Posted

    Ok, many thanks for your help!

    Ill try your suggestion to see if i can figure out what is wrong.

     

    BR

    • 0
  • Inquirer
  • Posted

    Weird, when using the API tester then it works..

    • 0
    Posted

    Copy paste reqbin json, but between single quotes ' ', try post raw string again.

    If doesn't work, then there must be some headers difference. 

    • 0
    Posted

    or you need to encode the json payload if it has special chars in it.

    • 0
    Posted (edited)

    Yep. reqbin raw shows you exactly what data it sends to remote server ...

     

    One difference is that it doesn't send Accepts header.

    Edited by ivx
    • 0
  • Inquirer
  • Posted

    Sorry, what do you mean by reqbin?

    • 0
    Posted (edited)

    I meant reqbin.com REST API tester.

     

    Also how you get token and call  

    Please login or register to see this link.

     . 

    Fibaro HTTP calls are asynchronous and execute in background.

    Without seeing code, are code free of racing condition ?

    Make call to get token

       - starts executing

    Now you make call to control lock, but get-token http request still executing(no token yet)

    Edited by ivx
    • 0
    Posted

    Or Postman (

    Please login or register to see this link.

    ), also a fine tool to test API connections. 

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...