Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: Using RestAPI with HTTPostRequest in DataFlex 17.1

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2009
    Location
    Kungsbacka, Sweden
    Posts
    837

    Default Using RestAPI with HTTPostRequest in DataFlex 17.1

    Hi!

    I have a request from a customer to implement some functions in VDF 17.1.

    In their end they have some sort of RestAPI. All the samples they sent me uses this software called CURL.

    One of the post operations does not return anything else but a simple ok result (Message 200 from the web).
    If issued in dos, it sets a cookie. If I do a HTTPPostRequest in VDF I would like to get the created cookie, but I just don't know how.

    An example of sending a login and getting a response back in form of a cookie (I guess it is used for a sessionticket) using the CURL

    Code:
    C:\Curl\curl -v -X POST -d "domain_id=texi&username=myname&password=mypassword" http://193.xxx.xxx.xxx:41900/Login
    The response from the server is:

    Code:
    * About to connect() to 193.xxx.xxx.xxx port 41900 (#0) * Trying 193.xxx.xxx.xxx... connected * Connected to 193.xxx.xxx.xxx (193.xxx.xxx.xxx) port 41900 (#0) > POST /login HTTP/1.1 > User-Agent: curl/7.16.4 (i386-apple-darwin9.0) libcurl/7.16.4 OpenSSL/0.9.7l zlib/1.2.3 > Host: 193.xxx.xxx.xxx:41900 > Accept: */* > Content-Length: 48 > Content-Type: application/x-www-form-urlencoded > < HTTP/1.1 200 OK < Set-Cookie: JSESSIONID=uw90urfx2j6c1samv49q4l3ae;Path=/ < Expires: Thu, 01 Jan 1970 00:00:00 GMT < Content-Length: 0 < Server: Jetty(8.0.4.v20111024) < * Connection #0 to host 193.xxx.xxx.xxx left intact * Closing connection #0
    So what I want to do is to use the HTTPPostRequest in VDF 17.1 if possible.

    How to get the JSESSIONID cookie that's returned?

    This world, with RestAPI, JSON is totally new to me.

    Any help, point to the right direction etc. is greatly appreciated.

    Regards
    Martin

  2. #2
    Join Date
    Feb 2009
    Location
    Maasland, The Netherlands
    Posts
    2,605

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Hi Martin,

    I have connected to a REST webservice and I am using a post of loginname and password to get a token.

    To test this REST webservice I use the Postman addin of Google Chrome. This helped me to communcate with the owner of the REST webservice. If you say that you are using Visual DataFlex, it will be Always your fault if it doesn't work .
    Best regards,

    Peter van Mil
    Appvantage b.v.

  3. #3
    Join Date
    Feb 2009
    Location
    Round Lake, IL
    Posts
    1,882

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Peter, could you show an example of how you do the post of loginname and password? I'm having trouble getting past the authentication.
    Todd Forsberg
    Idealease, Inc.
    Senior Programmer

    Web and Mobile Development: Think Outside the <DIV>

  4. #4
    Join Date
    Feb 2009
    Location
    Maasland, The Netherlands
    Posts
    2,605

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Hi Todd,

    The big trick of this specific REST service is getting the "x-wsse" header. This header is needed with all other function calls. See code. I don't know how other REST services work.

    Code:
    // 201 Returned when the token is successfully created 
    // 403 Returned when the supplied credentials are invalid
    
    // Parameters: username, password
    
    Function PkiLogin Returns tPkiLogin
      String  sFilePath sData sWsseCode
      Integer iRetVal iResponse
      
      tPkiLogin Login
      Set  psReturn to ""
      
      Move "/rest/token/new.json"                                       to sFilePath
      Move ("username=" + csPkiUserName + "&password=" + csPkiPassword) to sData
      Get  AddHeader "CONTENT-TYPE" "application/x-www-form-urlencoded" to iRetVal
      Get  HttpPostRequest sFilePath sData False to iRetVal
      
      Get  ResponseStatusCode to iResponse
      
      If (iResponse = 201) Begin
         Get ResponseHeader "x-wsse" 0 to sWsseCode
      End 
      Else Begin
         Move "" to sWsseCode
      End
      
      Move (iResponse = 201) to Login.bLoggedIn
      Move  iResponse        to Login.iResponse
      Move  sWsseCode        to Login.sWsseCode
      
      Function_Return Login
    End_Function
    
    //////////////////////////////////////////////////////
    
    Function PkiGetTheaters String sWsseCode Returns tPkiTheaters
    
    
    /////////////////////
    
          Move "/rest/theaters.xml" to sFilePath
          
          Get  AddHeader "CONTENT-TYPE" "application/json" to iRetVal
          Get  AddHeader "x-wsse"        sWsseCode         to iRetVal
          Get  HttpGetRequest sFilePath                    to iRetVal
    
    etc.
    Best regards,

    Peter van Mil
    Appvantage b.v.

  5. #5
    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! )

  6. #6
    Join Date
    Feb 2009
    Location
    Round Lake, IL
    Posts
    1,882

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Have you tried using Https (for secure RESTful services) ?
    Todd Forsberg
    Idealease, Inc.
    Senior Programmer

    Web and Mobile Development: Think Outside the <DIV>

  7. #7
    Join Date
    Feb 2009
    Location
    Maasland, The Netherlands
    Posts
    2,605

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Hi Todd,

    I didn't use it before, but I am struggling with it now. I was expecting, that setting the property piRemotePort to rpHttpSSL (=443) is sufficient. For some reason it doesn't work.

    Did you get any further?
    Best regards,

    Peter van Mil
    Appvantage b.v.

  8. #8
    Join Date
    Feb 2009
    Location
    Kungsbacka, Sweden
    Posts
    837

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    Hi!

    I got it to work, but i dont send it over Https. But if there is a Https, there should be a certificate involved, is it up to date on the remote computer, or do you need to install a certificate on your computer?

    There is a property for setting messages to see if a certificate is needed, right now i don't remember what the property is. I need to get back.
    Martin Arvidsson, Data Martin i Kungsbacka AB

    I have six locks on my door all in a row. When I go out, I lock every other one. I figure no matter how long somebody stands there picking the locks, they are always locking three.

  9. #9
    Join Date
    Feb 2009
    Location
    Maasland, The Netherlands
    Posts
    2,605

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    After searching this forum I have found it.

    You have to set these two properties together:

    Code:
    Set piRemotePort to rpHttpSSL
    Set peTransferFlags to ifSecure
    Last edited by Peter van Mil; 7-May-2014 at 08:32 AM.
    Best regards,

    Peter van Mil
    Appvantage b.v.

  10. #10
    Join Date
    Feb 2009
    Location
    Stockholm
    Posts
    71

    Default Re: Using RestAPI with HTTPostRequest in DataFlex 17.1

    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
    // Erik Svensson

Page 1 of 2 12 LastLast

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
  •