Wiki Home >> Using-HTTP-S-from-BASIC

Using-HTTP-S-from-BASIC


Simple HTTP GET Smoke Test

Before diving into complex JSON or header manipulation, you can perform a simple connectivity test to verify your N: device and network connection are working. This basic example opens a connection to Google and reads the first line of the response.

10 DIM U$(100)
20 U$="N:HTTP://GOOGLE.COM/"
30 PRINT "Testing connection to ";U$
40 TRAP 100
50 OPEN #1,4,0,U$ 
60 PRINT "Connection Successful!"
70 GET #1,B:PRINT CHR$(B);
80 IF B<>155 THEN 70
100 PRINT "Error: ";PEEK(195)
110 CLOSE #1
120 END
  • Line 10: Sets up a URL string variable.
  • Line 20: The N: devicespec for the connection.
  • Line 40: Sets a TRAP to handle errors (like Error 136 at the end of data).
  • Line 50: Opens the connection in mode 4 (Read).
  • Line 70: Reads one byte (GET) and prints it.
  • Line 80: Loops until a carriage return (155) is found, then proceeds to end.

HTTP GET with Headers

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 

70 CLOSE #1:POKE 766,0:END


## JSON Parsing (GET)

The following example demonstrates how to download a JSON object, parse it using the FujiNet JSON parser, and query specific fields.

10 DIM A$(256)
20 PRINT "DOWNLOADING JSON FROM:"
30 PRINT "JSONPLACEHOLDER.TYPICODE.COM"
40 OPEN #1,12,3,"N:HTTPS://JSONPLACEHOLDER.TYPICODE.COM/TODOS/1"
50 REM SET CHANNEL MODE TO JSON (AUX1=12)
60 XIO 252,#1,12,1,"N:"
70 REM PARSE THE JSON (AUX1=12)
80 XIO 80,#1,12,0,"N:"
90 REM QUERY FOR USERID
100 XIO 81,#1,12,0,"N:/userId"
110 INPUT #1,A$:PRINT "USER ID : ";A$
120 REM QUERY FOR ID
130 XIO 81,#1,12,0,"N:/id"
140 INPUT #1,A$:PRINT "ID : ";A$
150 REM QUERY FOR TITLE
160 XIO 81,#1,12,0,"N:/title"
170 INPUT #1,A$:PRINT "TITLE : ";A$
180 REM QUERY FOR COMPLETED
190 XIO 81,#1,12,0,"N:/completed"
200 INPUT #1,A$:PRINT "COMPLETED: ";A$
210 CLOSE #1
220 END


## Sending Data (POST)

To send data to a server (e.g., via a JSON API), you use `AUX1=13`. This requires a "clean" transmission pattern to avoid ATASCII character set issues and buffer synchronization problems.

A robust POST pattern in BASIC looks like this:

1.  **Open with `AUX2=2`** to ensure your line endings are Unix-compatible.
2.  **Use `XIO 77 ... 4`** to switch to POST data mode.
3.  **Use a semicolon (`;`)** at the end of your `PRINT` to prevent the Atari from adding an extra character.
4.  **Issue `XIO 15`** to flush the write buffer immediately.
5.  **Use `XIO 77 ... 0`** to switch to the response body.

For a full guide and a copy-pasteable template for JSON operations, see: Atari BASIC JSON POST Best Practices.

* 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.


## JSON Parsing (POST)

This example combines a JSON POST with parsing the response. **Note the critical transition from POST (13) to GET/Read (12) mode.**

> [!IMPORTANT]
> **Server Requirements**:
> *   Do **NOT** send `Transfer-Encoding: identity`. Use `Content-Length`.
> *   If returning large JSON bodies, use a `GET` loop instead of `INPUT` to avoid string overflow (Error 137).

10 REM --- SETUP ---
20 DIM URL$(80), BODY$(256), CK$(40)
30 DIM Q$(1), LB$(1), RB$(1), COL$(1)
40 Q$=CHR$(34):LB$=CHR$(123):RB$=CHR$(125):COL$=CHR$(58)
50 URL$="N:HTTP://192.168.1.49/INIT"
60 REM --- BUILD JSON: {"firstName":"MIKE"} ---
70 BODY$=LB$:BODY$(LEN(BODY$)+1)=Q$:BODY$(LEN(BODY$)+1)="firstName"
80 BODY$(LEN(BODY$)+1)=Q$:BODY$(LEN(BODY$)+1)=COL$
90 BODY$(LEN(BODY$)+1)=Q$:BODY$(LEN(BODY$)+1)="MIKE":BODY$(LEN(BODY$)+1)=Q$
100 BODY$(LEN(BODY$)+1)=RB$
110 REM --- 1. OPEN CONNECTION (POST MODE 13) ---
120 OPEN #1,13,2,URL$
130 REM --- 2. SET CONTENT-TYPE ---
140 XIO 77,#1,13,3,"N:":PRINT #1;"Content-Type: application/json"
150 REM --- 3. SEND BODY ---
160 XIO 77,#1,13,4,"N:":PRINT #1;BODY$;
170 REM --- 4. FLUSH (CRITICAL STEP!) ---
180 XIO 15,#1,12,2,"N:"
190 REM --- 5. SWITCH TO READ MODE (12) ---
200 XIO 77,#1,12,0,"N:"
210 STATUS #1,ST:REM CHECK STATUS TO WAKE BUFFER
220 REM --- 6. PARSE JSON (AUX1=12) ---
230 XIO 252,#1,12,1,"N:"
240 XIO 80,#1,12,0,"N:"
250 REM --- 7. EXTRACT COOKIE ---
260 XIO 81,#1,12,0,"N:/data/cookie"
270 TRAP 300:REM TRAP EOF
280 GET #1,B:PRINT CHR$(B);:GOTO 280
290 CLOSE #1:END
300 PRINT :PRINT "DONE":CLOSE #1:END





Wiki content is mirrored from the FujiNet Github Wiki