HTP. - HTTP Operations

<< Click to Display Table of Contents >>

Navigation:  3. Script Language > Internet and Network > HTP. - Command > !HTP-General - General HTTP operations and utilities > !Utility - Helper functions for HTTP operations (string conversion, proxy toggling) >

HTP. - HTTP Operations

HTP.ToBase64

Previous Top Next


SPR Script Language

 

HTP.ToBase64

Reads a local file and encodes its binary content into a Base64 string.

 

Intention

 

The HTP.ToBase64 command is essential for preparing binary file data (like images, audio, or documents) for transmission within text-based formats like JSON. Many modern APIs, such as OpenAI's Vision API, require images to be sent as a Base64-encoded string inside a JSON payload. This command reads the entire file into memory, encodes it, and returns the resulting single-line Base64 string.

 

It’s like your robot taking a digital photograph of a file's contents and converting it into a universal text code that can be safely sent anywhere.

 

Syntax

 

HTP.ToBase64|$$PTH[|$$RET]

 

Parameter Explanation

 

P1 - $$PTH - (Variable, String, Required)

The full path to the local file to be encoded.

 

P2 - $$RET - (Variable, String, Optional)

The variable where the resulting Base64-encoded string will be stored. If omitted, the result is placed on the top-of-stack (TOS).

 

Examples

 

'***********************************

' HTP.ToBase64 - Sample 1: Encode a text file

'***********************************

VAR.$$PTH=?path\my_file.txt

VAR.$$DAT=Hello World!

WFI.$$PTH|$$DAT

 

' Encode the file and store the result in $$B64

HTP.ToBase64|$$PTH|$$B64

 

' Verify the expected Base64 output

VAR.$$EXP=SGVsbG8gV29ybGQh

JIV.$$B64!$$EXP|Lab_failed

 

MBX.Success:|Base64 Result:$$crlf$$B64

DFI.$$PTH

END.

 

 

Remarks

 

- The output is a single, continuous string without any line breaks (CRLF).

- On failure (e.g., file not found), an empty string is returned and an error is set. Check HTP.GetErr for details.

 

Limitations

 

- This command reads the entire file into memory before encoding. For very large files (e.g., >50MB), this can consume significant memory. For such cases, use HTP.ToBase64File which streams the data.

 

See also:

 

HTP.FromBase64

HTP.ToBase64File

HTP.UploadFile