A JSON file is a text file so it's easy to downloadRoland Chastain wrote:Is it possible to call a web service which returns JSON data...
I don't see that operation in the examples.
but I don`t know what you mean with "web service"
Joshy
Code: Select all
{
"date":"2018-04-05",
"season":"easter",
"season_week":1,
"celebrations":[{
"title":"Pâques Jeudi",
"colour":"white",
"rank":"Jours liturgiques primaires",
"rank_num":1.2}],
"weekday":"thursday"
}
Code: Select all
' [S]imple [N]etwork [C]onnection
#include once "snc.bi"
#include once "snc_utility.bi"
'http://calapi.inadiutorium.cz/api/v0/fr/calendars/general-fr/today
' test of a client connection
const as string ServerName = "calapi.inadiutorium.cz"
const as string ServerPath = "/api/v0/fr/calendars/general-fr/"
const as string ServerFile = "today"
const as string FileType = MIME_TXT
' connect to web server at port 80
dim as NetworkClient client=type(ServerName,80)
' get a connection from ConnectionFactory
var connection = client.GetConnection()
' build an HTTP GET request
dim as string request =HTTPGet(ServerName,ServerPath & ServerFile,FileType)
' ready to send ?
while connection->CanPut()<>1
sleep 100
wend
' put data on the connection
connection->PutData(strptr(request),len(request))
' ready to receive ?
while connection->CanGet()<>1
sleep 100
wend
dim as zstring ptr buffer
var nBytes = connection->GetData(buffer)
print "number of received bytes " & nBytes
' get last char position of the HTTP asci header
var LastChar=instr(*buffer,HeaderEnd)-1
var Header =left(*buffer,LastChar)
' is it a OK answer ?
if instr(Header,"200 OK")<1 then
print *buffer
print "can't get " & ServerName & ServerPath & ServerFile & " !"
beep:sleep:end
end if
' get first byte behind the HTTP asci header
var DataStart=LastChar+4
' save it
open "test.json" for binary access write as #1
dim as ubyte ptr FileBuffer=@buffer[DataStart]
nBytes-=DataStart
put #1,,*FileBuffer,nBytes
close #1
print "file saved ..."
' free the buffer (allocate by snc.bi)
deallocate buffer
sleep