Using HTTP S from BASIC - FujiNetWIFI/fujinet-firmware GitHub Wiki

Here is a piece of BASIC code, which uses the HTTP protocol adapter to access a local Raspberry Pi web server (and its default index page). It manually sets a Basic Authorization header via SET HEADERS, and proceeds to ask for the Date and Content-Length headers via COLLECT HEADERS. It then switches to GET HEADER mode, which starts the HTTP transaction, and retrieves the Date and Content-Length headers, followed by the body.

For more information on the HTTP Set Mode 'M' command, you can see this page: https://github.com/FujiNetWIFI/fujinet-platformio/wiki/HTTP-Set-Channel-Mode

0 DIM A$(128)
1 POKE 766,1
10 OPEN #1,12,2,"N:HTTP://RASPBERRYPI/index.nginx-debian.html":REM HTTP GET
20 XIO 77,#1,12,3,"N:":REM SET HEADERS
30 PRINT #1;"Authorization: Basic dGhvbWM6ZTF4YjY0WEM0Ng=="
31 XIO 77,#1,12,1,"N:":REM COLLECT HEADERS
32 ? #1;"Date":? #1;"Content-Length"
33 XIO 77,#1,12,2,"N:":REM GET HEADERS
34 ? :? "Headers:":? 
35 ? "Date: ";:INPUT #1,A$:? A$
36 ? "Content-Length: ";:INPUT #1,A$:? A$
37 ? :? 
40 XIO 77,#1,12,0,"N:":REM GET BODY
50 TRAP 70
60 INPUT #1,A$:? A$:GOTO 60
70 CLOSE #1:POKE 766,0:END 
  • Line 0 sets up a temporary variable.

  • Line 1 temporarily sets DSPFLAG so that the { character does not accidentally clear the screen.

  • Line 10 opens the HTTP server in advanced GET mode (aux1=12), aux2=2 so that only LF is turned to EOL, this is because the nginx default html was created on Linux.

  • Line 20 uses the 'M' command (XIO 77) to set the channel mode to SET HEADERS

  • Line 30 writes the desired header to send, an authorization header.

  • Line 31 uses the 'M' command (XIO 77) to set the channel mode to COLLECT HEADERS

  • Line 32 writes "Date" and "Content-Length" to the channel to indicate that we want those two headers.

  • Line 33 uses the 'M' command (XIO 77) to set the channel mode to GET HEADERS, the HTTP transaction is immediately sent at this point.

  • Line 34 displays a "Headers" title

  • Line 35 displays a "Date:" header, followed by the content of the Date header

  • Line 36 displays a "Content-Length:" header, followed by the content of the Content-Length header, the size of the document body.

  • Line 37 adds additional line feeds to pad the display

  • Line 40 uses the 'M' command (XIO 77) to set the channel mode to GET BODY

  • Line 50 sets a trap for 70, to catch EOF (error 136) condition

  • Line 60 uses INPUT to read one line of the body, subsequently display it, and loop.

  • Line 70 closes the channel, which closes the protocol, sets DSPFLAG back to normal, and ends the program.