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" [URL]http://193.xxx.xxx.xxx:41900/Login[/URL]
[/CODE]
The response from the server is:
[CODE][INDENT]* 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[/INDENT]
[/CODE]
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
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 :(.
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.
Re: Using RestAPI with HTTPostRequest in DataFlex 17.1
Have you tried using Https (for secure RESTful services) ?
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.
[/CODE]
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?
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.
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
[/CODE]
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?
[LIST][*][URL]https://github.com/eriksven/VdfWebTools/blob/master/cJSONWebClient.pkg[/URL].[/LIST]
It handles http/https protocols, paths, encodings and querystrings for you. It's a subclass of the cWebClient, which is documented here: [URL]http://eriksven.com/visual-dataflex/web-client/[/URL]
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
[/CODE]
Re: Using RestAPI with HTTPostRequest in DataFlex 17.1
Hi Erik,
If I had to start today with a new project, I would choose for this. At this moment I have too much with XML to start all over. (Maybe will do it later anyway).