Skip to content

HTTPClient

HTTP client for making network requests Supports GET, POST, and custom requests with authentication

Methods

get()

javascript
get(url: string, callback: any): void

Make a GET request

Example:

javascript
http.get('https://api.example.com/data', function(result, error) {
    if (error) { console.error(error); return; }
    console.log(result.status, result.data);
});

Parameters:

  • url (string) - Request URL
  • callback (any) - Callback function (result, error)

Returns: void

post()

javascript
post(url: string, body: string, callback: any): void

Make a POST request

Example:

javascript
http.post('https://api.example.com/data', JSON.stringify({ name: 'test' }), function(result, error) {
    if (error) { console.error(error); return; }
    console.log(result.status, result.data);
});

Parameters:

  • url (string) - Request URL
  • body (string) - Request body string
  • callback (any) - Callback function (result, error)

Returns: void

request()

javascript
request(url: string, options: Array<any>, callback: any): void

Make a custom HTTP request

  • method: HTTP method (GET, POST, PUT, DELETE, etc.) - headers: Dictionary of HTTP headers - body: Request body string - token: Bearer token for Authorization header - username: Username for Basic auth - password: Password for Basic auth - responseType: Response format - "json" (default), "text", or "base64"

Example:

javascript
// JSON response (default)
var response = await http.request('https://api.example.com/data', {
    method: 'POST',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({ query: 'test' }),
    token: 'my-api-key'
});
console.log(response.data.results);

// Binary response as base64 (for audio, images, etc.)
var audio = await http.request('https://api.openai.com/v1/audio/speech', {
    method: 'POST',
    token: 'sk-...',
    headers: { 'Content-Type': 'application/json' },
    body: JSON.stringify({
        model: 'tts-1',
        input: 'Hello world',
        voice: 'nova',
        response_format: 'pcm'
    }),
    responseType: 'base64'
});
await entity.playAudioBuffer(audio.data, { sampleRate: 24000 });

Parameters:

  • url (string) - Request URL
  • options (Array<any>) - Request options
  • callback (any) - Callback function (result, error)

Returns: void