|
<< 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 |
SPR Script Language
HTP.UploadFile
Uploads a local file to a server using the multipart/form-data format.
Intention
The HTP.UploadFile command is designed for sending files to a web server, which is a common requirement for APIs like OpenAI's Speech-to-Text.
It constructs a multipart/form-data request, allowing you to include a file along with optional text-based form fields and custom headers.
This command handles the complex formatting of the request body automatically.
The command returns the raw server response in a variable and the HTTP status code on the top-of-stack (TOS).
Syntax
HTP.UploadFile|$$RTP|$$URL|$$PTH[|$$FLD|$$HED|$$FLG|$$UAG|$$RET]
Parameter Explanation
P1 - $$RTP - (Variable, String, Required)
The HTTP request type, typically "POST" or "PUT" for uploads.
P2 - $$URL - (Variable, String, Required)
The full target URL for the upload.
P3 - $$PTH - (Variable, String, Required)
The full local path to the file you want to upload.
P4 - $$FLD - (Variable, String, Optional)
A string of text-based form fields to send along with the file. Pairs are separated by "*" and key/value by "=". Example: "model=whisper-1*language=en".
P5 - $$HED - (Variable, String, Optional)
A string of custom headers, using the same format as $$FLD. An Authorization header is often required.
P6 - $$FLG - (Variable, numeric, Optional)
Numeric flags to modify request behavior. See HTP.Request for values.
P7 - $$UAG - (Variable, numeric, Optional)
A number from 0 to 30 to select a User-Agent. If omitted or -1, the global default is used.
P8 - $$RET - (Variable, String, Optional)
The variable to store the raw server response.
Examples
'***********************************
' HTP.UploadFile - Sample 1: OpenAI Speech-to-Text
'***********************************
VAR.$$URL=https://api.openai.com/v1/audio/transcriptions
VAR.$$PTH=?path\my_audio.mp3
VAR.$$KEY=sk-YourSecretKeyHere
' Define headers and form fields as strings
VAR.$$HED=Authorization=Bearer $$KEY
VAR.$$FLD=model=whisper-1*language=en
HTP.UploadFile|POST|$$URL|$$PTH|$$FLD|$$HED|||$$RES
' Check for success (TOS should be 200)
JIV.$$TOS!200|Lab_failed
' The raw response is in $$RES. Decode it for display.
HTP.FromUTF8|$$RES|$$VAL
MBX.Transcription:|$$VAL
END.
Remarks
- This command returns the HTTP Status Code on the top-of-stack ($$TOS). A value of 0 indicates a transport-level error. Check HTP.GetErr for details.
- The response body returned in $$RET is the raw, un-decoded byte string. Use HTP.FromUTF8 to decode it if the response is text.
- The library automatically handles the complex multipart boundary and Content-Type formatting.
See also: