Home  >   VTAP Platform   >  Helper Method for Server Scripts and Jobs - HTTP Request

Helper Method for Server Scripts and Jobs - HTTP Request

This article describes the HTTP Request helper method for Server Scripts and Jobs.
A
Abdul Sameer
1 Jul, 2024 - Updated 4 months ago
Table of Contents

HTTP Request Methods

To send HTTP requests to external service, helper methods are available. Methods for all type of requests are provided like GET, POST, PUT, PATCH, and DELETE.

 

Syntax

For each request type a method is available under vtap.macro.http library. For example to send GET request, you can use vtap.macro.http.get

Example
var response = await vtap.macro.http.get(https://www.mywebsite.com’);
 
Note: Methods are asynchronous and return a promise, therefore it must be used with await.
 

Parameters

All HTTP request methods expect 2 parameters - vtap.macro.http.(url, options)

  1. URL - Request URL (mandatory)
  2. Options - JSON object containing other request options (optional)
    • headers - Object containing request headers
    • qs - Object containing request query string values
    • body - Request body. String or JSON object if JSON is TRUE
    • form - Form data object
    • formData - Data to pass for a multipart/form-data request
    • JSON - Boolean. Sets body to JSON representation of value and adds Content-type: application or JSON header.
    • auth - Object, used for basic and bearer authentication.
 

Supported Methods

GET - vtap.macro.http.get

Method to send HTTP GET request

var response = await vtap.macro.http.get(https://www.mywebsite.com’, {
    headers: {
        Authorization: ‘XXXXXXXXXXXXXXXXXX’
    },
    qs: {
        key1: ‘Value1’,
        key2: ‘Value2’
    }
});
 

POST - vtap.macro.http.post

Method to send HTTP POST request

var response = await vtap.macro.http.post(https://www.mywebsite.com’, {
    headers: {
        CUSTOM_HEADER_KEY: ‘HEADER VALUE’
    },
    body: {
        key1: ‘Value 1,
        key2: ‘Value 2
    },
    json: true
});
 

PUT - vtap.macro.http.put

Method to send HTTP PUT request

var response = await vtap.macro.http.put(https://www.mywebsite.com’, {
    formData: {
        key1: ‘Value 1,
        key2: ‘Value 2
    }
});
 

PATCH - vtap.macro.http.patch

Method to send HTTP PATCH request

var response = await vtap.macro.http.patch(‘https://www/mywebsite.com’);
 

DELETE - vtap.macro.http.delete

Method to send HTTP DELETE request

var response = await vtap.macro.http.delete(‘https://www/mywebsite.com’);
 

GET - vtap.macro.http.get with basic auth

Method to send HTTP GET request with basic authentication
var response = await vtap.macro.http.get(https://www.mywebsite.com’, {
    headers: {
        Authorization: ‘XXXXXXXXXXXXXXXXXX’
    },
    qs: {
        key1: ‘Value1’,
        key2: ‘Value2’
    },
         auth: {
        username: ‘USERNAME’,
        password: ‘PASSWORD’
    },
});
 

GET - vtap.macro.http.get with bearer token

Method to send HTTP GET request with bearer authentication
var response = await vtap.macro.http.get(https://www.mywebsite.com’, {
    headers: {
        Authorization: ‘XXXXXXXXXXXXXXXXXX’
    },
    qs: {
        key1: ‘Value1’,
        key2: ‘Value2’
    },
         auth: {
        bearer: ‘BEARER_TOKEN’
    },
});

Constraints

  • Only HTTPS URLs with valid SSL certificates are allowed for sending requests.
  • Insecure URLs, direct IPs (e.g. 127.0.0.1), and localhost are not permitted.
  • Requests to any Vtiger’s domain are restricted, except for the instance URL where the code is executed.
  • To handle request failures, use try-catch blocks.
  • Place requests inside try blocks, and catch any errors for troubleshooting.
  • Request calls are asynchronous (i.e. script will not wait for the request to finish). To wait for requests to finish (wait for promise), you should use await.
  • Listening for promises using .then() is not supported.
Was this article helpful?
0  out of  0  found this helpful.
Comments 0
Be the first to comment
© Copyright 2023 Vtiger. All rights reserved.