[Stackoverflow] [Progress OpenEdge ABL] Progress 4GL GET request with cookie

Status
Not open for further replies.
K

KuKeC

Guest
I have to send some data to API but autorization to this api is thru cookie. So for sending data I have to send my username and password and in response I get cookie and in response body I get element "sessionID" which containts string value for that cookie. In every next API request I need to use that cookie (or create it with stored string value from first API request). How to make/integrate cookie functionality inside my existing procedures for API requests? If I do requests using Postman application, then it automaticly saves cookie from first request and use it on every next request.

So is solution for my problem :

  1. to save a cookie and use it for every next request?
  2. to save string value and create cookie for every next request?
  3. to store cookie into session and use cookie from session?
  4. something else I didn't thought of?

My procedure for sending GET request is below.

Code:
USING System.Xml.* FROM ASSEMBLY.
USING System.Net.* FROM ASSEMBLY.
USING System.Collections.Specialized.* FROM ASSEMBLY.

ROUTINE-LEVEL ON ERROR UNDO, THROW.

/* ***************************  Definitions  ************************** */

DEFINE INPUT  PARAMETER cLink       AS CHARACTER NO-UNDO.
DEFINE INPUT  PARAMETER cUser       AS CHARACTER NO-UNDO.
DEFINE INPUT  PARAMETER cPass       AS CHARACTER NO-UNDO.
DEFINE OUTPUT PARAMETER lcResponse  AS LONGCHAR  NO-UNDO.

DEFINE VARIABLE wcClient        AS System.Net.WebClient                               NO-UNDO.
DEFINE VARIABLE nvValues        AS System.Collections.Specialized.NameValueCollection NO-UNDO.
DEFINE VARIABLE whcResponse     AS System.Net.WebHeaderCollection                     NO-UNDO.
DEFINE VARIABLE rsResponse      AS "System.Byte[]"                                    NO-UNDO.
DEFINE VARIABLE cRespHeader     AS CHARACTER                                          NO-UNDO.
DEFINE VARIABLE wpProxy         AS System.Net.WebProxy                                NO-UNDO.

/* ***************************  Main Block  *************************** */

wcClient = NEW System.Net.WebClient().
nvValues = NEW System.Collections.Specialized.NameValueCollection(). 
ASSIGN 
    nvValues["username"]    = cUser
    nvValues["password"]    = cPass 
    .

rsResponse = wcClient:UploadValues(cLink, nvValues).
lcResponse = System.Text.Encoding:Default:GetString(rsResponse).
cRespHeader = System.Text.Encoding:Default:GetString(rsResponse).

DELETE OBJECT nvValues.
DELETE OBJECT rsResponse.
DELETE OBJECT wcClient.

Found some C# code for cookies but don't know how to integrate that into my procedure.

Code:
 CookieContainer cookieContainer = new CookieContainer();
 System.Net.Cookie userNameCookie = new System.Net.Cookie("user", "username");
 System.Net.Cookie passwordCookie = new System.Net.Cookie("password", "9848jf7s7ejhd");
 cookieContainer.Add(userNameCookie);
 cookieContainer.Add(passwordCookie);

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(produkt);
 request.CookieContainer = cookieContainer;

Thanks in advance

Continue reading...
 
Status
Not open for further replies.
Top