Page 2 of 2 FirstFirst 12
Results 11 to 18 of 18

Thread: Using RestAPI with HTTPostRequest in DataFlex 17.1

  1. #11
    Join Date
    Mar 2009
    Location
    São Paulo Brazil
    Posts
    162

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    HI Erik, thank you for share your code.


    I am testing cJSONWebClient.pkg and have sucess until now, but result of json is bigger than maximum length of string variables can support.


    Do you some tip do solve this ?




    Thank you again


    Regards


    Ezequiel Marin
    São Paulo / Brasil




    Quote Originally Posted by eriksven View Post
    I use this class for consuming RESTful webservices. Maybe it will do the trick for you?




    It handles http/https protocols, paths, encodings and querystrings for you. It's a subclass of the cWebClient, which is documented here: http://eriksven.com/visual-dataflex/web-client/

    Example requests:
    Code:
    Handle hClient
    String sJsonResponse sResponseHeader
    
    Get Create U_cJSONWebClient to hClient
    
    // Set request headers
    Set RequestHeader of hClient to "a-header-name" "a-header-value"
    
    // GET Request
    Get DownloadJSONString of hClient "https://some-webservice.com/records?id=234" to sJsonResponse
    
    // POST Request
    Get UploadJSONString of hClient "https://some-webservice.com/update" '{"id":234, "name":"test"}' to sJsonResponse
    
    // PUT Request
    Get PutJSONString of hClient "https://some-webservice.com/create" '{"id":234, "name":"test"}' to sJsonResponse
    
    // Retrieve response headers
    Get ResponseHeader of hClient "a-header-name" to sResponseHeader
    
    Send Destroy of hClient

  2. #12
    Join Date
    Feb 2009
    Posts
    5,467

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    set_argument_size ?
    Success consists of going from failure to failure without loss of enthusiasm - Winston Churchill

  3. #13

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Erik,

    We are using your parsing object to parse a single jsonrecord, but the next project must parse an array of json records.

    Example of current use...
    Handle hParser hDictionary
    Get Create U_cJSONParser to hParser
    Get Create U_cJSONDictionary to hDictionary
    Send Parse of hParser sJson hDictionary
    Get Value of hDictionary "first_name" to ResponseData.sFirstName
    Get Value of hDictionary "status" to ResponseData.sStatus

    how would that be done if sJason contains five records?
    Handle hParser hDictionary
    Get Create U_cJSONParser to hParser
    Get Create U_cJSONDictionary to hDictionary
    Send Parse of hParser sJson hDictionary
    Get Value of hDictionary "first_name" to ResponseData[iItem#].sFirstName
    Get Value of hDictionary "status" to ResponseData[iItem#].sStatus

    We know how to load up a struct array, but not how the parser handles sJson consisting of many records. Thanks for any guidance.

    Nick

  4. #14
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    2,805

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Hi Guys

    I do this all the time. In addition to setting piRemotePort of the cHTTPTransfer object to rpHttpSSL (actually 443) I also set peTransferFlags to ifSecure.

    I would advise standardising on using the new HttpVerbAddrRequest rather than using different commands (HttpPostRequest, HttpGetRequest, etc.) for different request types - it just makes your code more consistent and allows you to handle stuff in one place, however I don't think that became available until ~18.1.

    You can set the psUsername and psPassword properties of the HttpTransfer object to fulfill that requirement.

    Using HTTPS does not usually require a certificate at your end - that is required on the server offering the HTTPS endpoint.

    Take a look at some of the code in my OAuth2 or MSOffice365 samples (in the OAuth, RESTful & Web API Integration sub-forum) to see how to do this stuff, however please note that the JSON handling code in those is out of date as of 19.0, which introduced JSON objects to the product.

    For at least two reasons above you should ideally be moving out of 17.1 and onto 19.0+ for this stuff - the tools are just better!

    If you have other questions, just ask (but best in that forum).

    Mike (Dammit - this went to the wrong place in the thread! )

  5. #15
    Join Date
    Mar 2009
    Location
    Beech Hill - a village near Reading in the UK
    Posts
    2,805

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Guys

    Before we got JSON objects in 19.0, I used to do the following (I'm just typing this in from memory, not copying it from working code, so there will be typos!):

    Code:
    Use cHttpTransfer
    
    Object oHttp is a cHttpTransfer
        Property UChar[] pucaData
    
        Procedure OnDataReceived String sContentType String sData
            UChar[] ucaData
    
            Get pucaData to ucaData
            Move (AppendArray(ucaData, StringToUCharArray(sData))) to ucaData
            Set pucaData to ucaData
        End_Procedure
    
        Procedure Reset
            UChar[] empty
            
            Set pucaData to empty
        End_Procedure
    
    End_Object
    
    // elsewhere
    
    Send Reset of oHttp
    
    // Set up other stuff and make HTTP call of oHttp to bOK
    
    If bOK Begin
        Get ResponseStatusCode of oHttp to iStat
    
        If ((iStat >= 200) and (iStat < 300)) Begin
            Get pucaData to ucaResp
    
            Get_Argument_Size to iOldArgSize
    
            If (SizeOfArray(ucaData) > iOldArgSize) Begin
                Set_Argument_Size (SizeOfArray(ucaData) + 1024)  // Just being cautious!  ;-)
            End
    
            Move (UCharArrayToString(ucaData)) to sResponse
    
            // Do what needs to be done with the response
    
            Set_Argument_Size iOldArgSize
        End
     
        // Errors handling code for not bOK and duff HTTP status codes
    Which is a bit ugly, but lacking JSON objects was the best I could come up with.

    (Note: Get_Argument_Size has a "to"; Set_Argument_Size does not.)

    Mike

  6. #16
    Join Date
    Feb 2009
    Posts
    200

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Nick,

    Not sure if you've figured this out yet but this is how I do it. I get the 'ValueAtPath' to a variant array. From there I parse the fields as necessary (code posted below).

    Hope this help,

    Jeff

    Variant[] myList
    Integer i iCount
    Get ValueAtPath of hDictionary "myList" to myListMove (SizeOfArray(buList) -1) to iCount
    For i from 0 to iCount
    Get ValueAtPath of myList[i] "field1" to var1
    Get ValueAtPath of myList[i] "field2" to var2
    Get ValueAtPath of myList[i] "field3" to var3
    Loop

  7. #17
    Join Date
    Apr 2016
    Location
    Helsingborg, Sweden
    Posts
    85

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Hello Erik,

    I tried to use your code for consuming a RESTful webservice. The secong time I send the same Get request, the HTTPGetRequest returns 1 WITHOUT SENDING THE REQUEST.

    My code calls Get Create U_cJSONWebClient to hClient only once and keeps the handle in a property. Do I have to call it for each request? (with a destroy afterwards.) EDIT: This did not help. I have to exit the program and start it again.

    Code:
        Function _DoGetRequest String sURL Handle hTransfer Returns Boolean
            _tcWebClientUri Uri
            Integer iStatus
            Send _ClearDataReceived of (phTransfer(Self))
            Get _URISegments sURL to Uri
            Send _PrepareRequest Uri hTransfer
            Get HTTPGetRequest of hTransfer (Uri.Path + Uri.Name + If(Uri.Query <> "", "?" + Uri.Query, "") + If(Uri.Hash <> "", "#" + Uri.Hash, "")) to iStatus // This line does not send the same request twice.
            
            Function_Return (iStatus <> 0)
        End_Function
    /Lennart Thelander
    Last edited by Lennart_T; 22-May-2019 at 02:57 AM.

  8. #18
    Join Date
    Apr 2016
    Location
    Helsingborg, Sweden
    Posts
    85

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    I found this thread about the same problem I have:
    https://support.dataaccess.com/Forum...HTTPGetRequest

    The solution seems to be sending a bogus header like ?TimeSent2019-08-08-0655 and change it every time.

    There has to be a better way.
    Anybody?

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •