Charmed Quark Systems, Ltd. - Support Forums and Community
HTTP SendPUT - Printable Version

+- Charmed Quark Systems, Ltd. - Support Forums and Community (https://www.charmedquark.com/vb_forums)
+-- Forum: General Discussion (https://www.charmedquark.com/vb_forums/forumdisplay.php?fid=3)
+--- Forum: CQC Support (https://www.charmedquark.com/vb_forums/forumdisplay.php?fid=9)
+--- Thread: HTTP SendPUT (/showthread.php?tid=11023)



HTTP SendPUT - simplextech - 01-21-2020

I'm looking through the reference documentation on the SendPUT.

Code:
SendPUT
(
    [In]    MEng.System.Runtime.URL URL
    , [In]  MEng.Card4 WaitFor
    , [In]  MEng.String Agent
    , [In]  MEng.String Accept
    , [Out] MEng.String RepText
    , [Out] MEng.System.Runtime.HTTPClient.LinesList OutHdrLines
    , [InOu MEng.String ContType
    , [InOu MEng.System.Runtime.MemBuf Content
    , [InOu MEng.Card4 ContLen
    , [In]  MEng.Boolean OutBody
    , [In]  MEng.System.Runtime.HTTPClient.LinesList InHdrLines
)   Returns MEng.Card4;

I'm guessing the MEng.System.Runtime.MemBuf is the Body of the message (outbound) for the PUT?  This is a memory buffer and I was looking at the docs for that.  I'm not sure what would be correct for adding a JSON string to this.  Anyone know?  I'm trying to write a Macro to send a PUT to the Hue bridge to turn on/off Hue groups.


RE: HTTP SendPUT - simplextech - 01-21-2020

Sorta crude but it functions.  I need to add input fields for use with actions instead of hard coded values.  But the macro works.

Code:
Class=[NonFinal]
    ClassPath MEng.User.DoHTTPPut;
    ParentClass MEng.Object;
EndClass;

Imports=
    MEng.System.CQC.Runtime.CQCLogger;
    MEng.System.Runtime.HTTPClient;
    MEng.System.Runtime.URL;
    MEng.System.Runtime.KVPair;
EndImports;

Members=
    // An HTTP client to make server requests
    HTTPClient      m_HTTP;
    String          m_Credentials;
    CQCLogger       m_Log;
EndMembers;


Methods=[Public,Final]

    Constructor()
    Begin
    EndConstructor;



    Method Start([In] String URLStr, [Out] String RetCode) Returns Int4
    Begin
       
        Locals=
            LinesList   OutHdrLines;
            LinesList   InHdrLines;
            URL         URLToGet;
            String      RepText;
            String      ContType;
            MemBuf      DataBuf;
            Card4       ContLen;
            Card4       ResCode;
            KVPair      HdrPair;
            String      BodyText;
        EndLocals;

        Try
            BodyText := "{\"on\": false}";

            ContLen := DataBuf.ImportString(BodyText, Card4.kMaxValue);
            ContType := "application/json";
            InHdrLines.RemoveAll();

            //Set the URL to query
            m_Log.LogMsg1(BodyText, 0);
            URLToGet.Set(URLStr, FQTypes.Full);

            ResCode := m_HTTP.SendPUT
            (
                URLToGet
                , 5000
                , "CQSL/DoHTTPPut"
                , "application/json"
                , RepText
                , OutHdrLines
                , ContType
                , DataBuf
                , ContLen
                , True
                , InHdrLines
            );


            RetCode.Clear();
            RetCode.AppendCard4(ResCode, Radices.Dec);
        EndTry;

        Catch
            Return 0;
        EndCatch;

        Return 1;
    EndMethod;
EndMethods;



RE: HTTP SendPUT - Dean Roddey - 01-21-2020

If you just do an import string, it will be in ASCII form, so you should probably add an encoding indicator to the MIME format. You could also use a TextXCoder object to transcode it to UTF-8, which is the default for JSON. A lot of the time it won't matter, but you could end up with characters not representable as ASCII.


RE: HTTP SendPUT - simplextech - 01-21-2020

(01-21-2020, 02:22 PM)Dean Roddey Wrote: If you just do an import string, it will be in ASCII form, so you should probably add an encoding indicator to the MIME format. You could also use a TextXCoder object to transcode it to UTF-8, which is the default for JSON. A lot of the time it won't matter, but you could end up with characters not representable as ASCII.

I remembered that all of the CQC drivers were there for viewing so I took the SendPUT information straight from your existing Hue driver.

This will be a crude work around until the CQC Philips Hue driver supports groups.