Introduction
In this post, you will learn how to Parse Cookies from a Response and Pass Cookies to any Web Requests. SSIS PowerPack v1.8.2 introduced a new feature that allows sending and receiving cookies with HTTP Web Request/Response.
We will use the SSIS REST API Task to parse cookie values and then send cookies along with an HTTP Web Request. Cookies are beneficial for maintaining session state across multiple web requests.
Understanding Cookies
Let’s look at an example of how Cookie works. A cookie is a way of communication. Which means you can pass values to the Server or the Server can pass values to you.
Getting Cookies from Server (Web Response)
Many web APIs or sites send you cookies. If you are using a Browser, then it automatically saves for later use. The most common example is login pages. When you authenticate, the server sends cookies back via the Set-Cookie header. The server may send one or more Set-Cookie headers. When you use the SSIS REST API Task, you can map such a value on the Response Tab. However, one crucial difference is that the REST API Task will send only one Set-Cookie header, combining all cookies in Key1=Value1; Key2=Value2 … so on. You can use Cookie Tab to Parse each Cookie and save to a separate variable (Explained in the next section)
Web Request:
1 2 3 4 |
POST http://mysite.com/login Content-Type: application/x-www-form-urlencoded userid=SOME_USER_NAME&password=SOME_PASSWORD |
Web Response:
1 2 3 4 5 |
Set-Cookie: SitePreference=MobileVersion Set-Cookie: SessionId=12345 Set-Cookie: LoginToken=AAA111222; Expires=Wed, 09 Jun 2021 10:18:14 GMT ... ... |
Passing Cookies to Server (Web Request)
The user can pass one or multiple values in the form of Cookies to the server. To pass Cookie (s) to the Server, you have to send the Cookie header as follows
Web Request:
1 2 3 4 |
GET http://mysite.com/securepage.html Cookie: LoginToken=AAA1112222; SessionId=1234 ... ... |
Reading Cookies from Web Response using SSIS
SSIS REST API Task supports reading cookies in three different ways.
Method-1 (for SSIS / ODBC): Dynamic Token with Retain Cookie Setting (Using Cookie from Login)
In the new version of the HTTP Connection UI, you can find Retain Cookie settings as shown below.
If you are not receiving a Token from the response and only want to use Cookies, then set the following way (Enter Fake Regular expression (i.e., word like “SOME-FAKE” is fine )
Method-2 (for SSIS): Reading / Parsing Multiple Cookies (Save to SSIS variables)
If you like to save cookie value(s) into separate SSIS variable(s), then you can use SSIS REST API Task >> Cookie Tab to configure Mappings. Perform the following steps.
- Configure REST API Task with necessary information (e.g., URL, Headers, Body)
- Go to the Cookie Tab. In the Mapping Grid, enter each Cookie Name and map it to an SSIS Variable, as shown below. When you run the SSIS Package, you will see values stored in the variable.
Sometimes you may need to see all cookies in key/value pairs and save them to a variable. If that’s the case, then use the Response setting tab and map the Set-Cookie Response Header to some SSIS Variable.
Method-4 (for SSIS): Read Cookies as CookieContainer
Now, let’s look at how to use the CookieContainer Variable to save the Session across multiple API calls. All you have to do is create an Object datatype variable and select the same variable across various tasks.
In the example below, the first task is “Set Cookies”.It’s similar to a Login call in a traditional API call, where you get cookies back from the server. Call web url “http://httpbin.org/cookies/set?k2=val2&k1=myval1”, this generates some cookies and places them into a variable called “varCookies” (Must be Object datatype)
In the next step, we call the URL “http://httpbin.org/cookies” and pass the previously captured cookies from “varCookies” (Set on the Cookies tab). And the 3rd step displays the response from the URL (this demo URL returns all supplied cookies in JSON format).
Pass Cookies to Web API (for SSIS / ODBC)
Now, let’s look at how to Pass Cookie (s) along with your Web Request
To pass a cookie, you have to use the Cookie HTTP Header. You can pass multiple cookies in a single header as below
1 |
Cookie : Key1=value1; Key2=Value2 |
You can use an SSIS variable to pass a dynamic value, as shown in the screenshot below. (e.g. MyCookieName={{User::MyVariable}} )