Wiki Home >> Atari-BASIC-JSON-POST-Best-Practices

Atari-BASIC-JSON-POST-Best-Practices


Atari BASIC JSON POST Best Practices

Sending JSON via HTTP POST from Atari BASIC requires specific techniques to handle the character set differences and the way FujiNet handles network buffers.

Core Rules for Success

1. Handling Braces { }

The standard Atari keyboard does not have physical keys for the curly brace characters used in JSON. Even if you copy-paste them into a modern editor, the Atari's ATASCII character set will interpret them differently, or the BASIC interpreter may reject them.

To ensure your JSON is valid, you must use the CHR$ command:

  • Left Brace {: CHR$(123)
  • Right Brace }: CHR$(125)

2. Unix Translation Mode (AUX2=2)

There is a fundamental mismatch between the Atari and most modern web servers (Linux/Unix/Windows) regarding line endings:

  • Atari: Uses the EOL character (ATASCII 155).
  • Unix/Linux: Uses the Line Feed character (ASCII 10).

If you send a raw Atari EOL to a web server, it will often treat it as an illegal character, causing your request to fail.

  • When opening your connection, use AUX2=2. This tells the FujiNet N: handler to automatically intercept every ATASCII 155 and convert it to ASCII 10 before it leaves the hardware.
  • Example: OPEN #1,13,2,"N:HTTP://192.168.1.1/api"

3. Suppressing Automatic EOL (the semicolon ;)

By default, the PRINT command adds an EOL character to the end of your string. For JSON payloads, you often want to send exactly what you've built without extra characters.

  • Add a semicolon ; at the end of your PRINT statement to prevent the automatic EOL.
  • Example: PRINT #1;BODY$;

4. Forcing the Network Flush (XIO 15)

When you use a semicolon in a PRINT statement, FujiNet's buffer remains "open" because it hasn't seen a terminator yet. To ensure the server receives your data immediately:

  • Use XIO 15 to force a buffer flush.
  • Example: XIO 15,#1,12,0,"N:"

Clean POST Boilerplate

This example demonstrates a reliable pattern for sending a JSON object.

10 REM --- FUJINET CLEAN JSON POST ---
20 DIM URL$(80),BODY$(256),RESP$(256)
30 DIM Q$(1),LB$(1),RB$(1)
40 Q$=CHR$(34):LB$=CHR$(123):RB$=CHR$(125)
50 URL$="N:HTTP://192.168.1.49/endpoint"
60 REM 1. OPEN WITH AUX2=2 (UNIX MODE)
70 OPEN #1,13,2,URL$
80 REM 2. SET HEADERS IF NEEDED
90 XIO 77,#1,13,3,"N:"
100 PRINT #1;"Content-Type: application/json"
110 REM 3. SWITCH TO POST DATA MODE
120 XIO 77,#1,13,4,"N:"
130 REM 4. BUILD JSON MANUALLY
140 BODY$=LB$:REM LEFT BRACE
150 BODY$(LEN(BODY$)+1)=Q$:BODY$(LEN(BODY$)+1)="key":BODY$(LEN(BODY$)+1)=Q$
160 BODY$(LEN(BODY$)+1)=":":BODY$(LEN(BODY$)+1)=Q$:BODY$(LEN(BODY$)+1)="val":BODY$(LEN(BODY$)+1)=Q$
170 BODY$(LEN(BODY$)+1)=RB$:REM RIGHT BRACE
180 REM 5. PRINT WITH ; AND FLUSH
190 PRINT #1;BODY$;
200 XIO 15,#1,12,0,"N:"
210 REM 6. SWITCH TO READ RESPONSE
220 XIO 77,#1,13,0,"N:"
230 TRAP 260
240 INPUT #1,RESP$:PRINT RESP$
250 GOTO 240
260 CLOSE #1:END

Breakdown of Steps:

  • Line 40: Defines the JSON helper characters. LB$ (Left Brace), RB$ (Right Brace), and Q$ (Double Quote).
  • Line 70: Opens the channel with AUX2=2. This is the most important part for Unix/Linux server compatibility, as it translates Atari EOLs to standard Line Feeds.
  • Lines 90-100: Sets the Content-Type header to application/json.
  • Line 120: Uses XIO 77 ... 4 to signal that the next data written to the channel is the POST Body.
  • Lines 140-170: Builds the JSON string manually. Note the use of CHR$(123) and CHR$(125) (via LB$/RB$) since these characters aren't on the Atari keyboard.
  • Line 190: PRINT #1;BODY$; - The trailing semicolon is vital. It prevents the Atari from sending an extra EOL character that would invalidate the JSON.
  • Line 200: XIO 15 forces FujiNet to transmit the buffer immediately.
  • Line 220: XIO 77 ... 0 switches the channel from "Write Body" to "Read Response" mode. This is where the actual network transaction happens.
  • Line 240: Reads the response from the server.

Wiki content is mirrored from the FujiNet Github Wiki