Skip to content

Commit 9c60b63

Browse files
committed
v5.7.0
Added HeaderSubstitution setting
1 parent b8629e8 commit 9c60b63

File tree

3 files changed

+64
-5
lines changed

3 files changed

+64
-5
lines changed

docs/release-notes.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
## Version 5.7
2+
* Added shared setting [HeaderSubstitution](./request-settings.md#headersubstitution) to control if and how environment variables are injected into header names and/or values.
13
## Version 5.6
24
* Added support for content type "multipart/form-data". See [Content Types](./userguide.md#content-types).
35
## Version 5.5

docs/request-settings.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Request-related settings are settings you use to specify attributes of the HTTP request that `HttpCommand` will process.
2-
## Settings
2+
## Instance Settings
33
### `Command`
44
<table><tr>
55
<td>Description</td>
@@ -220,6 +220,57 @@ If <code>AuthType</code> is not set and <code>Auth</code> is set to either a cha
220220
<code> logged_in github.com 05-AUG-2022</code><br/>
221221
</td></tr></table>
222222

223+
## Shared Settings
224+
### `HeaderSubstitution`
225+
<table><tr>
226+
<td>Description</td>
227+
<td><p>In the following text, the phrase "environment variable" is taken to mean either an environment variable or a Dyalog configuration setting as both of these are retrieved using the same technique (<code>2 ⎕NQ '.' 'GetEnvironment')</code>.</p><p><code>HeaderSubstitution</code> provides a shorthand technique to inject environment variable values into the request's HTTP header names and values. If <code>HeaderSubstitution</code> is <code>''</code> (the default), no substitution is done. When <code>HeaderSubstitution</code> has a non-empty value, it denotes the beginning and ending delimiteres between which you may use the name of an enviroment variable. If <code>HeaderSubstitution</code> is a single character, that character is used as both the beginning and ending delimiter.</p>
228+
<p>You may also use the delimiters in the <a href="#auth"><code>Auth</code></a> setting as <code>Auth</code> is used to format the HTTP Authorization header.</p>
229+
</td></tr>
230+
<tr><td>Default</td>
231+
<td><code>''</code></td></tr>
232+
<tr><td>Example(s)</td><td>
233+
For these examples, assume we have an environment variable named "MyVariable" which has a value of <code>'0123456789'</code>. <pre><code> HttpCommand.HeaderSubstitution←'' ⍝ no substitutions done
234+
h←HttpCommand.New 'get' 'someurl.com'
235+
'name' h.SetHeader '%MyVariable%'
236+
h.Show
237+
GET / HTTP/1.1
238+
name: %MyVariable%
239+
Host: someurl.com
240+
User-Agent: Dyalog-HttpCommand/5.7.0
241+
Accept: */*
242+
Accept-Encoding: gzip, deflate</code></pre>
243+
<p>Now let's specify a delimiter...</p>
244+
<pre><code> HttpCommand.HeaderSubstitution←'%' ⍝ specify a delimiter
245+
h.Show
246+
GET / HTTP/1.1
247+
name: 0123456789
248+
Host: someurl.com
249+
User-Agent: Dyalog-HttpCommand/5.7.0
250+
Accept: */*
251+
Accept-Encoding: gzip, deflate
252+
</code></pre>
253+
<p>The delimiters do not have to be single characters...</p>
254+
<pre><code> HttpCommand.HeaderSubstitution←'env:[' ']'
255+
'name' h.SetHeader 'env:[MyVariable]'
256+
h.Show
257+
GET / HTTP/1.1
258+
name: 0123456789
259+
Host: someurl.com
260+
User-Agent: Dyalog-HttpCommand/5.7.0
261+
Accept: */*
262+
Accept-Encoding: gzip, deflate</code></pre>
263+
<p>Alternatively, you can use the <code>GetEnv</code> method to retrieve environment variables and/or Dyalog configuration settings.</p>
264+
<pre><code> 'name' h.SetHeader GetEnv 'MyAPIKey'
265+
</code></pre>
266+
</td></tr>
267+
<tr><td>Details</td>
268+
<td><p>Many web services require an API key. It is generally considered bad practice to hard-code such API keys in your application code. Storing the keys as environment variables allows them to be retrieved more securely.</p>
269+
<p>If no environment variable matches the name between the delimeters, no substitution is performed.</p>
270+
</td></tr>
271+
</table>
272+
273+
223274

224275
## Name/Value Pairs
225276
`Params`, for an appropriate content type, and `Headers` can be specified as name/value pairs. `HttpCommand` gives you some flexibility in how you specify name/value pairs. You may use:

source/HttpCommand.dyalog

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
rVersion
88
Return the current version
99
:Access public shared
10-
r'HttpCommand' '5.6.0' '2024-03-17'
10+
r'HttpCommand' '5.7.0' '2024-05-17'
1111
1212

1313
Request-related fields
@@ -20,6 +20,8 @@
2020
:field public Auth'' authentication string
2121
:field public AuthType'' authentication type
2222
:field public BaseURL'' base URL to use when making multiple requests to the same host
23+
:field public shared HeaderSubstitution'' delimiters to indicate environment/configuration settings be substituted in headers, set to '' to disable
24+
2325

2426
Proxy-related fields - only used if connecting through a proxy server
2527
:field public ProxyURL'' address of the proxy server
@@ -53,7 +55,6 @@
5355
:field public UseZip0 zip request payload (0-no, 1-use gzip, 2-use deflate)
5456
:field public ZipLevel1 default compression level (0-9)
5557
:field public shared Debug0 set to 1 to disable trapping, 2 to stop just before creating client
56-
5758
:field public readonly shared ValidFormUrlEncodedChars'&=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~*+~%'
5859

5960
:field Client'' Conga client ID
@@ -1355,9 +1356,14 @@
13551356
rHeaders
13561357
13571358

1358-
hdrsenvironment hdrs
1359+
hdrsenvironment hdrs;beg;end;escape;hits;regex
13591360
substitute any header names or values that begin with '$env:' with the named environment variable
1360-
hdrs(hdrs)'%[[:alpha:]].*?%'⎕R{GetEnv 1¯1.Match},hdrs
1361+
:If ~0HeaderSubstitution
1362+
(beg end)2HeaderSubstitution
1363+
escape'.^$*+?()[]{\|-'{m(1+t)¨1 tm\t t[~m]'\' t} chars that need escaping in regex
1364+
regex(escape beg),'[[:alpha:]].*?',escape end
1365+
hdrs(hdrs)regex ⎕R{0eGetEnv(beg)(-end).Match:.Match e},hdrs
1366+
:EndIf
13611367
13621368

13631369
hdrsprivatize hdrs

0 commit comments

Comments
 (0)