Request & Response

The core configuration of Machine Translation Request

Variables

  • $ID

  • $SOURCE_TEXT

  • $ORIGINAL_TEXT

  • $TRANSLATED_TEXT

  • $SOURCE_LANG

  • $TARGET_LANG

You can use schema variables in the configuration with syntax $SCHEMA_NAME For example:

{
    "schema": [
        { "type": "string", "name": "api_key", "title": "API Key", "default": "", "required": true, "message": "Required an API Key" },
    ],
    "request": {
        "method": "http_post",
        "url": "http://127.0.01/?key=$API_KEY"
        "options": {
            "headers": {
              "Authorization": "Bearer $API_KEY",
              "Content-Type": "application/json"
            }
        }        
    }
}

Parameters

method: string

  • web_scraping

  • http_get

  • http_post

  • web_llm

url: string

encodeURI: string (optional)

Escapes characters by UTF-8 code units, with each octet encoded in the format %XX, left-padded with 0 if necessary. Because lone surrogates in UTF-16 do not encode any valid Unicode character. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI

encodeURIComponent: string (optional)

Uses the same encoding algorithm as described in encodeURI. It escapes all characters except: A–Z a–z 0–9 - _ . ! ~ * ' ( ) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent

encodeURIExtra: string (optional)

Additional function to replace URI with regexp string. For example: "["%2F", "g", "\\%2F"]"

initialUrl: string (optional)

userAgent: string (optional)

querySelector: string

Return the first Element within the HTML that matches the specified selector, or group of selectors. https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector

querySelectorAll: string (optional)

return a static (not live) NodeList representing a list of the HTML elements that match the specified group of selectors. https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelectorAll

queryProperty: string

body: object

options: object (optional)

  • headers - set request headers

    • X-Custom-Header: string

    • Authorization: string

    • Content-Type: string

responseType: string

  • string - set response type as string

  • json - set response type as json

responseQuery: string


JSON Response from HTTP Request

You can parse the JSON Response with ResponseQuery

The responseQuery is only used for http_get and http_post methods with the response type as JSON

A JSON object is an associative array. The location path syntax to navigate into an arbitrarily deeply nested structure of JSON objects comprises the field names separated by dot '.' delimiters.

For example the following JSON Response

{
  "text": "Hello 1",
  "words": [
    { "text": "Hello 2" },
    { "text": "Hello 3" }
  ]
  "lines": {
    "text": "Hello 4",
    "sub": {
      "text": "Hello 5"
    }        
  }
}
responseQuery
Result

text

Hello 1

words[0].text

Hello 2

lines.sub.text

Hello 5



Examples

1. Web Scraping Method - Object Structure

  • method: string

  • encodeURI: boolean

  • encodeURIComponent: boolean

  • encodeURIExtra: string

  • url: string

  • initialUrl: string

  • querySelector: string

  • querySelectorAll: string

  • queryProperty: string

{
    "request": {
        "method": "web_scraping",
        "encodeURI": false,
        "encodeURIComponent": true, 
        "initialUrl": "https://localhost:8080",
        "url": "https://localhost:8080/?sl=$SOURCE_LANG&tl=$TARGET_LANG&text=$SOURCE_TEXT",    
        "querySelector": "span[lang=$TARGET_LANG]",
        "querySelectorAll": null,                
        "queryProperty": "innerText"
    }
}

2. HTTP GET Method- Object Structure

  • method: string

  • encodeURI: boolean

  • encodeURIComponent: boolean

  • encodeURIExtra: string

  • url: string

  • options: object

  • responseType: string

  • responseQuery: string

{
    "request": {
        "method": "http_get",
        "encodeURI": false,
        "encodeURIComponent": false, 
        "url": "https://localhost:8080/translate?sl=$SOURCE_LANG&tl=$TARGET_LANG&text=$SOURCE_TEXT",
        "options": {},
        "responseType": "string"  
    }
}

3. HTTP POST Method - Object Structure

  • method: string

  • encodeURI: boolean

  • encodeURIComponent: boolean

  • encodeURIExtra: string

  • url: string

  • body: object

  • options: object

    • headers: object

      • X-Custom-Header: string

      • Authorization: string

      • Content-Type: string

  • responseType: string

  • responseQuery: string

{
    "request": {
        "method": "http_post",
        "encodeURI": false,
        "encodeURIComponent": false, 
        "url": "https://localhost:8080/translate",
        "body": {
            "text": "$SOURCE_TEXT",
            "source_lang": "$SOURCE_LANG",
            "target_lang": "$TARGET_LANG"
        }
        "options": {
            "headers": {
                "Authorization": "Bearer $API_KEY",
                "Content-Type": "application/json"
            }
        },
        "responseType": "json",
        "responseQuery": "translate.result"
    }
}

Last updated