Connecting with Rest API

Overview

This documentation explains how to connect to Chochae Engine REST API using a POST request with a JSON payload. The example uses a specific endpoint and includes the necessary headers and body data.

API Endpoint

Webhook API

  • Method: POST

  • URL: https://ccgpt-uat.iapp.co.th/common/webhook-api?id=YOUR_ID

Headers

NameTypeDescription

Content-Type

String

Specifies the media type of the resource. Set to application/json

Request Body

The request body is in JSON format. In this example, it contains a message in Thai.

{
  "message": "สวัสดี"
}

Example Code Using Axios

Import Axios

First, import the Axios library to make HTTP requests:

const axios = require('axios');

Create Data Payload

Define the data to be sent in the POST request as a JSON string. In this example, the payload contains a message in Thai:

let data = JSON.stringify({
  "message": "สวัสดี"
});

Configure the Request

Set up the Axios request configuration, including the HTTP method, URL, headers, and data payload:

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://ccgpt-uat.iapp.co.th/common/webhook-api?id=YOUR_ID',
  headers: { 
    'Content-Type': 'application/json'
  },
  data: data
};
  • method: HTTP method for the request (POST in this case).

  • maxBodyLength: Configures Axios to handle large payloads.

  • url: Endpoint to which the request is sent.

  • headers: HTTP headers for the request, specifying the content type as application/json.

  • data: JSON payload to be sent.

Send the Request

Use axios.request to send the configured request. Handle the response and potential errors with then and catch blocks:

axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});
  • .then((response) => { ... }): Executes if the request is successful, logging the response data.

  • .catch((error) => { ... }): Executes if there is an error during the request, logging the error.

Full Code Example

javascriptCopy codeconst axios = require('axios');

let data = JSON.stringify({
  "message": "สวัสดี"
});

let config = {
  method: 'post',
  maxBodyLength: Infinity,
  url: 'https://ccgpt-uat.iapp.co.th/common/webhook-api?id=YOUR_ID',
  headers: { 
    'Content-Type': 'application/json'
  },
  data: data
};

axios.request(config)
.then((response) => {
  console.log(JSON.stringify(response.data));
})
.catch((error) => {
  console.log(error);
});

Example Response

200: OK Sample Response

If the request is successful, you might receive a response similar to the following:

{
    "question": "[{'role': 'user', 'content': 'สวัสดี'}]",
    "response": [
        {
            "text": "-----\nสวัสดีครับ ผมเป็นเจ้าหน้าที่ตอบคำถาม ผมพร้อมให้บริการและตอบคำถามทุกข้อสงสัยของคุณ หากคุณมีคำถามหรือข้อสงสัยใดๆ โปรดไม่ลังเลที่จะถามผม ผมจะพยายามตอบคำถามของคุณให้ได้มากที่สุดและให้ข้อมูลที่มีประโยชน์ที่สุดครับ",
            "type": "text"
        }
    ]
}

for the video tutorial please click : https://ccgpt-uat.iapp.co.th/public/video/api.mp4

Last updated