Skip to content

Commit 7661928

Browse files
authored
#57 Allow GetJSON to take just a URL argument (#58)
1 parent 499725b commit 7661928

File tree

2 files changed

+13
-47
lines changed

2 files changed

+13
-47
lines changed

docs/shortcut-methods.md

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -41,55 +41,18 @@ Intermediate positional parameters that are not used should be set to <code>''</
4141
</table>
4242

4343
### `GetJSON` - issue a request to a JSON-based web service
44-
`GetJSON` is used to interact with web services that use JSON for their request and response payloads. It was originally developed as a convenient way to interact with [Jarvis](https://githib.com/dyalog/Jarvis), Dyalog's JSON and REST Service framework. Conveniently, it turns out that there are many web services, including GitHub and Twitter, that use JSON as well.
44+
`GetJSON` is used to interact with web services that use JSON for their request and response payloads. It was originally developed as a convenient way to interact with [Jarvis](https://github.com/dyalog/Jarvis), Dyalog's JSON and REST Service framework. Conveniently, it turns out that there are many web services, including GitHub and Twitter, that use JSON as well.
4545

4646
When `Command` is something other than `GET` or `HEAD`, `GetJSON` will automatically convert APL `Params` into JSON format. For `GET` and `HEAD`, `HttpCommand` will URLEncode `Params` in the query string of the `URL`. The rationale behind this is that `GET` and `HEAD` requests should not have content; therefore `Params` should be included in the query string of `URL` and it doesn't make a lot of sense to include JSON in the query string. If you really need JSON in the query string, you can use `⎕JSON` to convert `Params`.
4747

4848
`GetJSON` will attempt to convert any JSON response payload into its equivalent APL representation.
4949

50-
<table>
51-
<tr><td>Syntax</td>
52-
<td><code>r ←{RequestOnly} HttpCommand.GetJSON args</code></td></tr>
53-
<tr><td><code>args</code></td>
54-
<td>Either<ul><li>a vector of positional settings (<code>Command</code> and <code>URL</code> are required.)<br/>
55-
<code><a href="./request-settings#Command">Command</a>
56-
<a href="./request-settings#URL">URL</a>
57-
<a href="./request-settings#Params">Params</a>
58-
<a href="./request-settings#Headers">Headers</a>
59-
<a href="./conga-settings#Cert">Cert</a>
60-
<a href="./conga-settings#SSLFlags">SSLFlags</a>
61-
<a href="./conga-settings#Priority">Priority</a>
62-
</code><br/>
63-
Intermediate positional parameters that are not used should be set to <code>''</code></li>
64-
<li>A namespace containing named variables for the settings for the request.<br/>
65-
</li></ul></td></tr>
66-
<tr><td><code>RequestOnly</code></td>
67-
<td>(optional) A Boolean indicating:<ul>
68-
<li><code>0</code> - (default) send the HTTP request and return the response result.
69-
<li><code>1</code> - return the formatted HTTP request that HttpCommand <i>would</i> send if <code>RequestOnly</code> was <code>0</code>.</li></ul></td></tr>
70-
<tr><td><code>r</code></td>
71-
<td>If RequestOnly is<ul><li><code>0</code> - a namespace containing the request response.</li>
72-
<li><code>1</code> - the formatted HTTP request that <code>HttpCommand</code> would send if <code>RequestOnly</code> was <code>0</code>.</td></tr>
73-
<tr><td>Example(s)</td>
74-
<td>These examples assume you have a <a href="https://github.com/dyalog/Jarvis"><code>Jarvis</code></a> service running at <code>http://localhost:8080</code> and a endpoint of <code>#.sum ← {+/⍵}</code>.<br/><br/>
75-
<code> args ← ⎕NS ''
76-
args.Command ← 'post'
77-
args.URL ← 'localhost:8080/sum'
78-
args.Params ← ⍳1000
79-
⊢r ← HttpCommand.GetJSON args
80-
[rc: 0 | msg: | HTTP Status: 200 "OK" | ⍴Data: ⍬]
81-
r.Data
82-
500500
83-
84-
Params ← ('per_page' '3')('page' '1')('sort' 'full_name')
85-
URL ← 'https://api.github.com/orgs/dyalog/repos'
86-
⊢r ← HttpCommand.GetJSON 'get' URL Params
87-
[rc: 0 | msg: | HTTP Status: 200 "OK" | ⍴Data: 3]
88-
89-
r.Data.full_name
90-
Dyalog/a3s-Linux Dyalog/APLCourse Dyalog/aplssh
91-
</td></tr>
92-
</table>
50+
|--|--|
51+
| Syntax | `r ←{RequestOnly} HttpCommand.GetJSON args` |
52+
| `args` | One of:<ul><li>a simple character vector [`URL`](./request-settings.md#url)</li><li>a vector of positional settings (`Command` and `URL` are required.)<br/>[`Command`](./request-settings#command)<br/>[`URL`](./request-settings#url)<br/> [`Params`](./request-settings#params)<br/> [`Headers`](./request-settings#headers)<br/> [`Cert`](./conga-settings#cert)<br/>[`SSLFlags`](./conga-settings#sslflags)<br/>[`Priority`](./conga-settings#priority)<br/>Intermediate positional parameters that are not used should be set to `''`</li><li>A namespace containing named variables for the settings for the request.</li></ul>|
53+
| `RequestOnly` | (optional) A Boolean indicating:<ul><li>`0` - (default) send the HTTP request and return the response result.<li>`1` - return the formatted HTTP request that HttpCommand *would* send if `RequestOnly` was `0`.</li></ul>|
54+
| `r` | If `RequestOnly` is<ul><li>`0` - a namespace containing the request response.</li><li>`1` - the formatted HTTP request that `HttpCommand` would send if `RequestOnly` was `0`.</li></ul>|
55+
| Example(s) | These examples assume you have a [`Jarvis`](https://github.com/dyalog/Jarvis) service running at `http://localhost:8080` and a endpoint of `#.sum ← {+/⍵}`.<br/>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;`args ← ⎕NS ''`<br/>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;`args.Command ← 'post'`<br/>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;`args.URL ← 'localhost:8080/sum'`<br>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;`args.Params ← ⍳1000`<br/>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;`⊢r ← HttpCommand.GetJSON args`<br/>`[rc: 0 | msg: | HTTP Status: 200 "OK" | ⍴Data: ⍬]`<br/>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;`r.Data`<br/>`500500`<br/><br/>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;`Params ← ('per_page' '3')('page' '1')('sort' 'full_name')`<br/>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;`URL ← 'https://api.github.com/orgs/dyalog/repos'`<br/>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;`⊢r ← HttpCommand.GetJSON 'get' URL Params`<br/>`[rc: 0 | msg: | HTTP Status: 200 "OK" | ⍴Data: 3]`<br/><br/>&ensp;&ensp;&ensp;&ensp;&ensp;&ensp;`r.Data.full_name`<br/>` Dyalog/a3s-Linux Dyalog/APLCourse Dyalog/aplssh`|
9356

9457
### `Do` - issue a generic HTTP request
9558
`Do` is essentially the same as `Get` except that you specify the HTTP method (`Command`) to use.

source/HttpCommand.dyalog

Lines changed: 6 additions & 3 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.8.0' '2024-07-17'
10+
r'HttpCommand' '5.9.0' '2025-02-24'
1111
1212

1313
Request-related fields
@@ -190,9 +190,12 @@
190190

191191
r{requestOnly}GetJSON args;cmd
192192
Shared method to perform an HTTP request with JSON data as the request and response payloads
193-
args - [Command URL Params Headers Cert SSLFlags Priority]
193+
args - [URL] | [Command URL Params Headers Cert SSLFlags Priority]
194194
:Access public shared
195-
:If 0=⎕NC'requestOnly' requestOnly¯1 :EndIf
195+
:If 0=⎕NC'requestOnly' requestOnly¯1 :EndIf
196+
197+
:If isSimpleChar args simple character vector args?
198+
:AndIf (args'localhost')/argsover lc ⎕A args'GET'args :EndIf localhost or only alphabetics?
196199

197200
∆EXIT9.1=nameClass cmdrequestOnly New args
198201
:If 0cmd.Command cmd.Command(1+0cmd.Params)'POST' 'GET' :EndIf

0 commit comments

Comments
 (0)