Page 1 of 1

APL using CURL

Posted: Thu Apr 23, 2020 11:40 pm
by Gantois
Hi!

Are there some documentation or example about APL using CURL?
How can I read/take data from url and put into an apl variable?
The data is JSON. I need put it into apl variables to update my apl files.

Sending from my system/MiServer to app using var←⎕cmd "CURL command ...." is ok. My difficulty is in the opposite way.

Could someone help me, please?

Cheers,
Marco

Re: APL using CURL

Posted: Fri Apr 24, 2020 4:51 pm
by petermsiegel
Can you easily share a test case (i.e. simplified example) of what you are trying that isn't working? I'm not an expert and, if it matters, I'm using a Mac.

Re: APL using CURL

Posted: Sat Apr 25, 2020 2:22 pm
by Gantois
Hi Peter,
Thanks for your response. Im Marco Gantois from Brazil. Nice to meet you.
I think my problem can be solved with a similar function structure as MiServer Calss but without returning to browser, i.e:

:Class apitest: blank
:Field Public action
:Field Public urldata

∇ Render req; response
:Access Public
response←''
DoAction
req.Return response


∇ DoAction

:Select action
:Case '01'
....
:Case '02'
....
:EndSelect

:EndClass


Thanks,
//MG

Re: APL using CURL

Posted: Sun Apr 26, 2020 6:54 pm
by Brian|Dyalog
Hi Marco,

If I understand your question, you'd like to be able to return JSON data from MiServer in response to a request from curl?

I also see you're using MiServer version 2, so I'll respond with examples using that version rather than Version 3.

The first question is: Is your data static (not computed dynamically)? If so, you can simply put the JSON data in a .json file in your MiSite and any client (like curl) can just retrieve it.

So if you have a file, let's call it data.json for this example, that looks like:

Code: Select all

{
    "name":"Drake Mallard",
    "age":42
}

Then it can be retrieved just by asking for the file:
      ⎕SH'curl localhost:8080/data.json'
{
"name":"Drake Mallard",
"age":42
}

If your JSON data is dynamic, then you can write a MiPage that returns the data.

Code: Select all

:Class jsonPage : MildPage                                          
                                                               
    ∇ Render req;jsonData                                       
      :Access Public                                           
      jsonData←⎕NS ''                       ⍝ create a namespace
      jsonData.(name age)←'Drake Mallard' 42 ⍝ populate with some data
      jsonData←⎕JSON jsonData                ⍝ convert to JSON format
      'content-type' 'application/json'req.Return jsonData
      req.Response.NoWrap←1                  ⍝ don't "wrap" the page                                   
     ∇                                                         
                                                               
:EndClass                                                       

This line sets the content-type header and just returns the raw data

Code: Select all

      'content-type' 'application/json'req.Return jsonData

and this line tells MiServer not to do any additional processing on the response

Code: Select all

      req.Response.NoWrap←1                   ⍝ don't "wrap" the page

Then the page can be retrieved using curl as follows:
      ⎕SH'curl localhost:8080/jsonPage'
{"age":42,"name":"Drake Mallard"}


I hope this helps!
/Brian

Re: APL using CURL

Posted: Sun Apr 26, 2020 11:11 pm
by Gantois
Hi Brian, How are you?
Thanks for your response.

Yes, I am using MiServer version 2 and you gave me informations extremely useful, that I didn't know, and certainly I will use: "convert to JSON format" (very nice).

I'm develloping an endpoint to receive informations from an app. This app (PHP/MySql) send a JSON to MiServer (post method) with some pre-defined fields, one of them is a "transaction_type".

I need get these fields from JSON to apl variables and then update my components files according to type of transaction.

I think you've already clarified one of my doubt:
1. I do not need give back info from MiServer to app. The answer is:
req.Response.NoWrap←1, isn't it?

2. JSON to apl variables.
I was thinking to devellop functions to do this but your response about APL to JSON make me think that there are functions or instructions to do this (JSON to APL) that could help me avoiding unnecessary develloping. Are there this opposite way?

Thanks very much,
Cheers,
Marco

Re: APL using CURL

Posted: Mon Apr 27, 2020 4:16 am
by Brian|Dyalog
Hi Marco,

If your endpoint doesn't need a user interface (like a browser), then you might want to consider Jarvis, which is the successor to JSONServer. Jarvis/JSONServer makes it extremely easy for apps/clients to exchange data with Dyalog APL. We've done a number of webinars about JSONServer and microservices in Dyalog. If this sounds interesting to you, please contact mailto:?subject=&body= directly and we can set up a time for me to show you more about Jarvis.

/Brian

Re: APL using CURL

Posted: Mon Apr 27, 2020 12:19 pm
by Gantois
Ok Brian,
I have already sent an email to you,
Thanks
Marco