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 > !Request-Management - Commands for preparing, executing, and inspecting HTTP requests >

HTP. - HTTP Operations

HTP.FromBase64

Previous Top Next


SPR Script Language

 

HTP.FromBase64

Decodes a Base64 encoded string into its original raw binary data.

 

Intention

 

The HTP.FromBase64 command is used to convert a text string that has been Base64-encoded back into its original binary form. This is essential when working with APIs (like OpenAI's Image Generation API) that return binary data, such as images or audio files, encoded as a text string within a JSON response. The command intelligently handles common formatting issues like whitespace or line breaks in the input string.

 

It’s like your robot’s universal decoder ring—it takes a secret text message from an API and turns it back into a usable file or raw data.

 

Syntax

 

HTP.FromBase64|$$B64[|$$RET]

 

Parameter Explanation

 

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

The variable containing the Base64-encoded string to be decoded.

 

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

The variable to store the resulting raw (binary) string. If omitted, the result is placed on the top-of-stack (TOS).

 

Examples

 

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

' HTP.FromBase64 - Sample 1: Decoding simple text

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

VAR.$$B64=SGVsbG8gV29ybGQh

HTP.FromBase64|$$B64|$$RAW

MBX.Decoded:|$$RAW ' Displays "Hello World!"

END.

 

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

' HTP.FromBase64 - Sample 2: Decoding a Base64 image from a JSON response

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

' Assume $$RES contains a JSON response like:

' {"data":[{"b64_json":"iVBORw0KGgoAAAANSUhEUgAAAA..."}]}

VAR.$$RES={"data":[{"b64_json":"iVBORw0KGgoAAAANSUhEUgAAAAUA..."}]}

VAR.$$PTH=data[0].b64_json

 

PSJ.Parse|$$RES|$$H_1

PSJ.GetStr|$$H_1|$$PTH|$$B64

PSJ.Free|$$H_1

 

HTP.FromBase64|$$B64|$$RAW

' $$RAW now contains the raw binary data of the PNG image

WFI.?path\my_image.png|$$RAW

MBX.Success:|Image saved to my_image.png

END.

 

 

Remarks

 

- The output is a raw binary STRING. If the original data was text, it will be displayable. If it was an image or other binary file, it will not be human-readable and should be saved to a file using WFI.

- The command is very robust and automatically ignores common whitespace characters (spaces, tabs, newlines) and other invalid characters that might be present in the Base64 input.

 

Limitations

 

- If the input Base64 string is corrupt or has an invalid length that cannot be padded, the command will fail and return an empty string. Check HTP.GetErr to see the reason.

 

See also:

 

HTP.ToBase64

HTP.ToBase64File