Webhook Relay
PricingLogin
  • Introduction
  • Quick Start - Forwarding
  • Quick Start - Tunnels
  • πŸ› οΈInstallation
    • Relay CLI
      • Install
      • Auto-start
      • Run config.yaml reference
    • Containerized
      • Kubernetes Installation
      • Podman
      • Docker
      • Docker Compose
  • Products
    • πŸ›°οΈWebhook Forwarding
      • Glossary
      • WebSocket Server
      • Authentication
      • Custom Domains
    • ⚑Functions
      • Managing functions
      • Edit request/response
      • Working with JSON
      • 🦾Advanced
        • Working with time
        • Testing functions in CLI
        • Making HTTP Requests
        • Multipart Form Data
        • URLEncoded Form Data
        • GCP BigQuery
        • Sending Emails
        • JWT authentication
        • Base64, Hashes, Encryption
      • πŸ€–Integrating into CI/CD
    • πŸ”ƒTunnels
      • Using tunnels
      • Custom Domains
      • Encryption (HTTPS)
      • Regions
  • πŸ“Examples
    • Intro to examples
    • Webhooks
      • Receiving webhooks on localhost
      • Receive webhooks inside your JavaScript app
      • Execute shell scripts on remote machines
    • Functions
      • Enrich webhooks from 3rd party APIs
      • Convert DockerHub webhook to Slack notification
      • Allowing only POST requests through
      • Manipulate webhook request body
    • Tunnels
      • Ingress for any Kubernetes environment
      • Demoing your website
    • 🏠Home Automation
      • Home Assistant
      • Node-RED
      • Raspberry Pi
  • Platform
    • CLI Basics
    • Using CLI Behind Proxy
    • Self-hosting Server
      • Server deployment
      • Client configuration
    • Security & Tech
Powered by GitBook
On this page

Was this helpful?

  1. Products
  2. Functions

Working with JSON

JSON is a popular format in which services exchange information. Functions allow parsing and modifying these payloads to integrate different services with each other.

To decode or encode JSON in a Lua function, import β€˜json’ package:

-- import "json" package when working with JSON
local json = require("json")

-- example payload:
-- {
--   "user": "Peter",
--   "age": 25,
--   "city": "Edinburgh"
-- }

local request_payload, err = json.decode(r.RequestBody)
if err then error(err) end

-- now, request_payload is a normal JSON object and we
-- can access individual values

r:SetRequestBody(request_payload.user)
-- request will now have a single value 'Peter' in the body

To encode a structure back into a JSON string:

-- import "json" package when working with JSON
local json = require("json")

-- constructing a new object that we will encode
-- into a JSON string
local new_payload = {
    action= "hello", 
    message= "world",
}

-- encoding
local encoded_payload, err = json.encode(new_payload)
if err then error(err) end

r:SetRequestBody(encoded_payload)
-- webhook request body is now changed to:
-- {
--   "action": "hello",
--   "message: "world"
-- }
PreviousEdit request/responseNextAdvanced

Last updated 2 years ago

Was this helpful?

⚑