HTTP GET, POST, PUT and DELETE

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !

HTTP GET, POST, PUT and DELETE

Postby woody on Thu Feb 25, 2021 3:13 am

Greetings.

I'm using Conga and the HTTPReq function from Dyalog 17.0.34605 Samples namespace to make some Web Service calls to my stock broker API to buy/sell stock in real time (too much fun!).

If anyone is interested in learning how to automate buying and selling of stock and stock options with APL ... let me know... I'd be happy to share what I've learned.

The HTTPReq GET and POST method work great.

I need to also be able to send PUT and DELETE HTTP methods.

Example in CURL:
https://documentation.tradier.com/brokerage-api/trading/cancel-order


Code: Select all
r←{certs}HTTPReq args
          ⍝ issue an HTTP GET or POST request
          ⍝ certs - optional PublicCert PrivateKey SSLValidation
          ⍝ args  - [1] URL in format [HTTP[S]://][user:pass@]url[:port][/page]
          ⍝         {2} parameters is using POST - either a namespace or URL-encoded string
          ⍝         {3} HTTP headers in form {↑}(('hdr1' 'val1')('hdr2' 'val2'))
          ⍝ Makes secure connection if left arg provided or URL begins with https:

          ⍝ Result: (return code) (HTTP headers) (HTTP body) [PeerCert if secure]
 ⍝ Call Samples namespace
 r←#.Samples.HTTPReq args


Below is the version of HTTPReq I think I'm using.

It seems that I could modify HTTPReq to re-use the GET: section of the program ... but somehow pass or "flag" the need to use PUT or DELETE rather than GET.

Any ideas on how best to achieve this?

Sincere thanks!

//W

Code: Select all
 r←{certs}HTTPReq args;U;DRC;fromutf8;h2d;getchunklen;eis;getHeader;addHeader;makeHeaders;fmtHeaders;url;parms;hdrs;b;p;secure;port;host;page;x509;flags;priority;pars;auth;req;err;chunked;chunk;buffer;chunklength;header;datalen;data;done;wr;len;cmd;ref;found;split
          ⍝ issue an HTTP GET or POST request
          ⍝ certs - optional PublicCert PrivateKey SSLValidation
          ⍝ args  - [1] URL in format [HTTP[S]://][user:pass@]url[:port][/page]
          ⍝         {2} parameters is using POST - either a namespace or URL-encoded string
          ⍝         {3} HTTP headers in form {↑}(('hdr1' 'val1')('hdr2' 'val2'))
          ⍝ Makes secure connection if left arg provided or URL begins with https:

          ⍝ Result: (return code) (HTTP headers) (HTTP body) [PeerCert if secure]
 fromutf8←{0::(⎕AV,'?')[⎕AVU⍳⍵] ⋄ 'UTF-8'⎕UCS ⍵} ⍝ Turn raw UTF-8 input into text
 h2d←{⎕IO←0 ⋄ 16⊥'0123456789abcdef'⍳U.lc ⍵} ⍝ hex to decimal
 getchunklen←{¯1=len←¯1+⊃(NL⍷⍵)/⍳⍴⍵:¯1 ¯1 ⋄ chunklen←h2d len↑⍵ ⋄ (⍴⍵)<len+chunklen+4:¯1 ¯1 ⋄ len chunklen}
 eis←{⍺←1 ⋄ ,(⊂⍣(⍺=|≡⍵))⍵} ⍝ enclose if simple
 getHeader←{(⍺[;2],⊂'')⊃⍨⍺[;1]⍳eis ⍵}
 addHeader←{0∊⍴⍺⍺ getHeader ⍺:⍺⍺⍪⍺ ⍵ ⋄ ⍺⍺}
 makeHeaders←{⎕ML←1 ⋄ 0∊⍴⍵:0 2⍴⊂'' ⋄ 2=⍴⍴⍵:⍵ ⋄ ↑2 eis ⍵}
 fmtHeaders←{⎕ML←1 ⋄ 0∊⍴⍵:'' ⋄ ∊{NL,⍨(1⊃⍵),': ',⍕2⊃⍵}¨↓⍵}
 split←{(p↑⍵)((p←¯1+⍵⍳⍺)↓⍵)}
 args←eis args

 (url parms hdrs)←args,(⍴args)↓''(⎕NS'')''

 :If 326=⎕DR parms ⍝ if parms are a namespace, format them
     parms←{0∊⍴t←⍵.⎕NL ¯2:'' ⋄ 1↓⊃,/⍵{'&',⍵,'=',(⍕⍺⍎⍵)}¨t}parms
 :EndIf

 cmd←(1+0∊⍴parms)⊃'POST' 'GET' ⍝ set command based on whether we've passed POST parameters

 r←¯1(0 2⍴⊂'')''

 :For ref :In ## #
     :If found←3=ref.⎕NC'Conga.Init' ⋄ DRC←ref.Conga.Init''              ⍝ v3 intialisation
     :ElseIf found←9.1=ref.⎕NC⊂'DRC' ⋄ DRC←ref.DRC ⋄ {}DRC.Init'' ⋄ :EndIf ⍝ Pre-v3 NS
     →found⍴GET
 :EndFor

 ⎕←'Could not find Conga or DRC object'
 →0
⍝⍝⍝⍝⍝ SNIP ⍝⍝⍝⍝⍝⍝
Woodley Butler
Automatonics, Inc.
"Find your head in the APL Cloud"
http://www.APLcloud.com
User avatar
woody
 
Posts: 144
Joined: Tue Dec 28, 2010 12:54 am
Location: Atlanta, Georgia USA

Re: HTTP GET, POST, PUT and DELETE

Postby Brian|Dyalog on Thu Feb 25, 2021 2:23 pm

Hi Woodley!

You should be using HttpCommand. It's been installed with Dyalog APL for several versions now. To load it into your workspace, simply do one of the following:
      ]load HttpCommand ⍝ from your APL session
⎕SE.SALT.Load 'HttpCommand' ⍝ under program control

HttpCommand.Documentation ⍝ will display the documentation

If you're composing complicated requests, the technique I recommend is to create an instance of HttpCommand, assign the appropriate fields, and then run it. For example:
      cmd←⎕NEW HttpCommand
cmd.URL←'https://api.tradier.com/v1/accounts/{account_id}/orders/{order_id}'
cmd.Headers←(('Authorization' 'Bearer <TOKEN>')('Accept' 'application/json'))
cmd.Command←'DELETE'
result←cmd.Run

result is a namespace with a number of elements regarding the request and response. The payload (in this case JSON) from the response is found in result.Data.

You can run subsequent commands by reassigning the fields in cmd and doing cmd.Run again. There's a lot more to HttpCommand that you can find in the documentation.

I hope this helps!
/Brian
User avatar
Brian|Dyalog
 
Posts: 116
Joined: Thu Nov 26, 2009 4:02 pm
Location: West Henrietta, NY

Re: HTTP GET, POST, PUT and DELETE

Postby woody on Thu Feb 25, 2021 10:44 pm

Excellent!

Thanks a million Brian!

I will upgrade and share knowledge of same.

Cheers,

//W
Woodley Butler
Automatonics, Inc.
"Find your head in the APL Cloud"
http://www.APLcloud.com
User avatar
woody
 
Posts: 144
Joined: Tue Dec 28, 2010 12:54 am
Location: Atlanta, Georgia USA


Return to APL Chat

Who is online

Users browsing this forum: No registered users and 1 guest