|
<< 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.ReqShort
Performs a simplified HTTP request with minimal parameters, ideal for API calls.
Intention
The HTP.ReqShort command is a high-level wrapper for making common HTTP requests, especially to JSON-based APIs. It simplifies the process by requiring only the essential parameters and using globally configured settings for timeouts, proxy, and user agent. It automatically handles UTF-8 conversion for payloads and responses.
The command returns the decoded text response in a variable or on the stack, and the final HTTP status code (e.g., 200) or an HTP error code on the top-of-stack (TOS). This makes it easy to check for success and process the result.
Syntax
HTP.ReqShort|$$RT|$$URL[|$$DATA|$$APIKEY|$$RET]
Parameter Explanation
P1 - $$RT - (Variable, String, Required)
The HTTP request type. Valid values are "GET", "POST", "PUT", "DELETE", etc. See HTP.GetReq.
P2 - $$URL - (Variable, String, Required)
The full URL to send the request to (e.g., "https://httpbin.org/post").
P3 - $$DATA - (Variable, String, Optional)
The payload data for methods like POST or PUT (e.g., a JSON string). If omitted, no data is sent.
P4 - $$APIKEY - (Variable, String, Optional)
An API key for authorization. If provided, it overrides the global key set by HTP.SetDef for this call only.
P5 - $$RET - (Variable, String, Optional)
The variable to store the decoded response body. If omitted, the response is not stored in a variable.
Examples
'***********************************
' HTP.ReqShort - Sample 1: Simple GET Request
'***********************************
VAR.$$URL=https://httpbin.org/get
HTP.ReqShort|GET|$$URL|||$$RES
' $$TOS will contain 200 on success
' $$RES will contain the JSON response body
JIV.$$TOS!200|Lab_failed
PSJ.Parse|$$RES|$$H_1
PSJ.GetStr|$$H_1|url|$$VAL
MBX.URL from Response: $$VAL
PSJ.Free|$$H_1
'***********************************
' HTP.ReqShort - Sample 2: POST with Payload and API Key
'***********************************
VAR.$$URL=https://api.openai.com/v1/chat/completions
VAR.$$KEY=sk-YourSecretKeyHere
VAR.$$DAT={"model": "gpt-4o", "messages": [{"role": "user", "content": "Hello!"}]}
HTP.ReqShort|POST|$$URL|$$DAT|$$KEY|$$RES
JIV.$$TOS!200|Lab_failed
PSJ.ExtractChoicesContent|$$RES|$$VAL
MBX.AI Response:$$crlf$$VAL
Remarks
- This command returns the HTTP Status Code (e.g., 200, 404, 500) on the top-of-stack ($$TOS). A value of 0 indicates a transport-level error (e.g., could not connect). Check HTP.GetErr for details on failure.
- The response body returned in $$RET is automatically decoded from UTF-8.
- If an API key is provided (either via the $$APIKEY parameter or globally with HTP.SetDef), the required Content-Type and Authorization headers are added automatically.
Limitations
- For complex requests requiring custom headers, specific flags, or file uploads, use the more advanced HTP.Request or HTP.UploadFile commands.
See also: