Get text (string) from a http answer

Einklappen
X
 
  • Zeit
  • Anzeigen
Alles löschen
neue Beiträge
  • sennevb
    Dumb Home'r
    • 21.03.2021
    • 22

    #1

    Get text (string) from a http answer

    I have a virtual output, which is getting info from an api, i am saving the answer in a textfile: /user/common/token.txt
    I need to get a part of the json (the actual token) from the txt file.
    I used a virtual input, but it isnt recognizing the text, but from what i've read this is not the way to get a string value, outputs are only numeric the way i am using it.

    Someone any clue how i can get a part of the json stored as a variable in string format?
  • Tico
    Lox Guru
    • 31.08.2016
    • 1035

    #2
    Text retrieval is not supported natively in the Miniserver. There is a work-around that can be used for small text passages (~50 characters) -


    I assume you have worked out how to poll the file saved at /user/common/token.txt. If not, here is another link that describes the address required -

    Code:
    http://Username:Password@MiniserverIP:Port/dev/fsget/user/common/token.txt​
    Moin, ich habe gestern (versucht) herauszufinden, wie und ob ich mit dem Miniserver gen2 einen API (mit Token) abfragen kann. Bisher habe ich gelesen, dass Loxone bei den virtuellen Eingängen keinen header für den Token mitsenden kann. Demnach ist eine Abfrage der API nicht möglich. Aber vielleicht habt ihr ja noch einen Tipp
    Ich spreche kein Deutsch. Gib Google Translate die Schuld, wenn ich unverständlich bin.

    Kommentar

    • sennevb
      Dumb Home'r
      • 21.03.2021
      • 22

      #3
      Yes i figured how the polling works.
      I'm going to try your workaround and see how it goes

      Update: I got the token extracted now, took me 32 bytes :-)
      Zuletzt geändert von sennevb; 07.05.2023, 17:19.

      Kommentar

      • sennevb
        Dumb Home'r
        • 21.03.2021
        • 22

        #4
        Hello,

        Here i am again.
        I managed to extact the token using the workaround, now i'm bumping into the next issue:
        I put the result of the workaround into a marker, and forward this to a virtual output

        Seems it's not working, i used debug to check whats wrong (see image)
        Look like the forwarded text is limited to 25 characters, it cuts of the first x characters

        I also tried a status block, but where the <v1> value is inserted, theres no string, altough liveview shows the string being forwarded to the virtual output.

        Anyone any clue? Tico maybe?

        Kommentar

        • Tico
          Lox Guru
          • 31.08.2016
          • 1035

          #5
          It would be helpful to see a screenshot of your Virtual Output. There are two options for building the lines 'Command for On', 'HTTP Header for On' or 'HTTP body for ON' -

          Option 1. In the 1st example pictured below, <v> is injected where the token should go within the line. The Virtual Output command is being fed just the token text. In this example, a fully formed 1740 character string is arriving in the Miniserver from an external system (your 32 bytes shouldn't be a problem).

          Option 2. An alternative is to pre-form the line in a Status Block and forward the complete line to the Virtual Output.

          In the 2nd picture below, Option 2 would have a Status Text column with the text -
          Code:
          Content-Type: application/json \r\nx-token: <v2>
          The XToken is fed to I2 of a Status Block and a Trigger on I1. The Status Block text output then goes to the Virtual Ouput and would have the 'HTTP Header for On' as simply <v>.

          Sometimes one option works where the other option fails. Importantly, in both cases, 'Use as digital output' must be unchecked. Also ensure your memory flags are set to type 'Text'.

          The second option also provides a 'gate', whereby the token text is prevented from flowing through to the Virtual Output command until it is triggered. The text workaround takes a finite time to build the complete 32 byte string. The Virtual Output will fire when it sees a value change. It sounds like yours might be firing prematurely...
          Zuletzt geändert von Tico; 09.05.2023, 01:32.
          Ich spreche kein Deutsch. Gib Google Translate die Schuld, wenn ich unverständlich bin.

          Kommentar

          • sennevb
            Dumb Home'r
            • 21.03.2021
            • 22

            #6
            See screenshot in attachment.
            Will try your 2 options in the meantime and let know how it goes Klicke auf die Grafik für eine vergrößerte Ansicht

Name: screens.png
Ansichten: 721
Größe: 25,7 KB
ID: 390777

            Kommentar

            • sennevb
              Dumb Home'r
              • 21.03.2021
              • 22

              #7
              Update:

              The trigger seems to do the trick, i made a trigger wich i currently can trigger from the app.
              Trigger is I2 in status block (see image), and only sends a value when I2 = 1

              It works, but sends a 0 when the trigger is 0.
              So 2 virtual outputs are now send, the right value when the trigger is 1, and a 0 when the trigger is 0.
              it seems to translate the empty text value to a 0..

              I think i'll leave this, bcuz it doesnt harm the working..
              Angehängte Dateien

              Kommentar

              • sennevb
                Dumb Home'r
                • 21.03.2021
                • 22

                #8
                Question: it is impossible to send 2 variables to a virtual output??
                i need 1 value in the header and 1 in the body...

                Kommentar

                • Tico
                  Lox Guru
                  • 31.08.2016
                  • 1035

                  #9
                  I don't know of way to have a variable header and a variable body. The most complex Virtual Output I use has three fixed variables.

                  HTTP Header for On -
                  Code:
                  Content-Type: application/json
                  HTTP Body for On
                  Code:
                  {"api_key":"aaaabbbbccccdddd","email":"me@gmail.com","password":"xxxxyyyyzzzz"}
                  But that doesn't address your problem...

                  The only path I can think of is to experiment with sending the HTTP Body for On as <v> and fully composing the two variables in a Status Block. ie -

                  Status Text column = \r\nxtoken : <v1>\r\n{"content_of_body":"<v2>"}

                  Highly unlikely to work but you never know.
                  Ich spreche kein Deutsch. Gib Google Translate die Schuld, wenn ich unverständlich bin.

                  Kommentar

                  Lädt...