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

Call to external API REST


t.coli
 Share

Question

4 answers to this question

Recommended Posts

  • 0
  • Inquirer
  • How ? 

     

    i trie this example code but....

     

    HC2 = Net.FHttp("https://jsonplaceholder.typicode.com") 
    response, status, errorCode = HC2:GET('/todos/') 
    fibaro:log(response) 
    fibaro:sleep(3000)    
      if errorCode == 0 
      then 
        fibaro:log(status) 
      else 
        fibaro:log("error") 
      end

     

    i would like to read json value from this response in a fibaro virtual device 

     

     

    i tried also this code

    HC2 = Net.FHttp("jsonplaceholder.typicode.com",80)

    fibaro:debug('Button LUA Triggered: ')

    response ,status, errorCode = HC2:GET("/todos")

    jsonTable = json.decode(response)

    fibaro:debug('errorCode: ' .. errorCode);
    fibaro:debug('status: ' .. status);
    fibaro:debug('jsonTable: ' .. jsonTable);

    fibaro:log("ready")

    fibaro:debug('Button LUA Finished: ')

     

    but don't work  ?

    Edited by t.coli
    Link to comment
    Share on other sites

    • 0
    On 10/16/2020 at 8:07 PM, Bodyart said:

    Yes :-D

     

    Can you be more useful and provide some MWE?

    Link to comment
    Share on other sites

    • 0

    Hello @t.coli ,@Tazovsky ,

     

    To make a request to HTTPS (secure) endpoints you have to use scene on HC2 since virtual devices are not able to do such calls. Here is an example of scene code that can fetch data and also send data:

    Please login or register to see this code.

     

    Above example first call function createData() with method POST which adds new record and then if record is added response.status == 201 then calls function getData() with method GET which fetches first record from the /posts endpoint.

     

    Since each HTTP call in scenes opens separate thread (scene code continues to execute next line of code even HTTP call is not finished yet) you have to write code to handle received data inside the functions that make https call as indicated in the above code.

     

    Now if you need to run scene to first get some data and then show received data on VD labels you will need to run scene and then wait until scene stops running and then continue with VD code. For example:

    Please login or register to see this code.

    Loop above will wait for scene all instances to stop running or if it loops for more than 2 minutes then will also stop. This time check is added as safety so that VD code still continue running if scene never stops.

    Above VD code ensures that latest received data will be shown on VD. Without waiting for scene to stop running VD will still show previous data and not the data from the last scene call.

     

    Hope this helps! Enjoy coding :-)

     

     

    • Like 1
    Link to comment
    Share on other sites

    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.

     Share

    ×
    ×
    • Create New...