Request and Response Object - Serverless Functions

Request and Response Object - Serverless Functions

You can get the entire Request Object within the function using the "crmAPIRequest" argument.

Say you've created a function and defined 2 arguments. Now you need to use the same function in 2 different webhooks, each of which might contain different types and amount of information. One of them uploads data as a JSON object and the other one uploads a CSV file. Now those two are different types of data and may not be available in an argument inside the function.

In this case, you can make use of the crmAPIRequest argument to get the corresponding content.



The "crmAPIRequest" argument can be used to get the data from the "Body", "Parameter", "Header" or "User" information. With this single argument, the need to create multiple arguments within the function is not needed, as it dynamically stores the information from the request and makes it available inside the function.

Note
  • You don't have to manually pass values to the crmAPIRequest. The CRM maps the request object to the argument.
  • Still, if any value is passed for the crmAPIRequest argument, it will be overidden by the request info.

A sample output from POSTMAN


Response object

The user can define how the response of the API is going to be. If the user wants to display/get a particular response code as the response of the function, he can specify it within the function.

In addition to status codes, the user can also choose to get the response in a specific file type, such as a JSON, HTML, Text, etc.



The crmAPIResponse in the function serves as the argument that lets you determine the type and the content of the output response.

Status Code

There are only a few status codes that are usually used to identify if the API call is successful or if it a bad request. You can define the status code to be displayed for the API call.

The default status code is 200.

To set the status code

  1. /**
  2. Your Business Logic here
  3. **/
  4. response = Map();
  5. // to override the status code to 204.
  6. response.put("status_code",204);
  7. return {"crmAPIResponse":response};

Content-Type

In addition to status codes, the response which is usually given as a JSON object can be set to be obtained in a different format.

The default value of this key is application/json;charset=utf-8.

To get the response in text format

  1. /**
  2. Your Business Logic here
  3. **/
  4. response = Map();
  5. // to override the content type, default is application/json
  6. response.put("Content-Type","application/text");
  7. return {"crmAPIResponse":response};

Headers

Response headers are useful in defining the size and type of file/data that the API call gives out as a response. In some cases, people prefer to view the response headers, since they are useful in determining the next course of action, i.e, next API call.

The default value of this key is {"Content-Disposition", "attachment;filename=response.json"}.

To get the response headers

  1. /**
  2. Your Business Logic here
  3. **/
  4. response = Map();
  5. headers = Map();
  6. headers.put("X-ZOHO-SOURCE","CRM");
  7. headers.put("X-Frame-Options","SAMEORIGIN");
  8. headers.put("X-RATELIMIT-LIMIT","60");
  9. response.put("headers",headers);
  10. return {"crmAPIResponse":response};

Body

Response body will contain the information that you need to send to the 3rd party as a response to their request.

The default value of body is empty.

To get the response headers

  1. /**
  2. Your Business Logic here
  3. **/
  4. response = Map();
  5. body = "{<XML>}"
  6. response.put("body",body);
  7. return {"crmAPIResponse":response};

    • Related Articles

    • Overview - Serverless Functions

      Serverless architecture, also known as “Function-as-a-service”(Faas), provides a platform for developers to execute their own codes in response of various business events. In Zoho CRM, all these codes can be written through deluge scripts and can be ...
    • Authentication - Serverless Functions

      Authentication Method A serverless function within your CRM can be invoked from any third-party application or within the CRM. However, commonly not all of the applications support a single authentication method. Keeping that in mind, we have two ...
    • Using API Key - Serverless Functions

      Introduction A severless function within your CRM can be invoked from any third-party application or within the CRM using a webhook. However, generally most of the webhooks do not support OAuth2, save for a few of them. In that case, you can execute ...
    • Types of Inputs - Serverless Functions

      The input for the function can be acquired in the following types Parameters Parameters are passed in URL of query string and the data passed in the post "form-data" section in an input. The "params" in the crmAPIRequest map contains these parameters ...
    • Setting up Functions

      All custom functions programmed by your Developers are listed in the Functions page. By this way, programmers can focus on the developing Deluge Scripts where as CRM Administrators can deploy the thoroughly tested custom functions as per requirement. ...