Send a batch of events in a single HTTP request. Recommended when the client knows in advance the number of events to send. Anatomy of the request Code Block |
---|
POST <endpoint>/stream/<domain>/token!<token>/<host>/<tag> HTTP/1.1
Host: <host>
Content-Length: <body length>
<message 1>\n
<message 2>\n
...
<message n>\n |
Examples Code Block |
---|
const https = require('https');
const URL_HOST = process.env.URL_HOST;
const URL_PORT = process.env.URL_PORT || 443;
const TOKEN = process.env.TOKEN;
const DOMAIN = process.env.DOMAIN;
const HOST = process.env.HOST;
const TAG = process.env.TAG;
const ACCEPT_UNAUTHORIZED = process.env.ACCEPT_UNAUTHORIZED !== undefined;
const options = {
hostname: URL_HOST,
port: URL_PORT,
path: `/stream/${DOMAIN}/token!${TOKEN}/${HOST}/${TAG}`,
method: 'POST',
headers: {
'Content-Type': 'text/plain',
},
rejectUnauthorized: !ACCEPT_UNAUTHORIZED
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', chunk => console.log(`Response: ${chunk}`));
});
req.on('error', err => console.error(err));
const batch = createBatch('This is Sparta!', 10);
req.end(batch);
function createBatch(message, n) {
return Array.from({length: n}, (_, i) => `${message} ${i + 1}`).join(`\n`);
} |
Code Block |
---|
import http.client
import ssl
import os
URL_HOST = os.environ.get("URL_HOST")
URL_PORT = os.environ.get("URL_PORT", 443)
TOKEN = os.environ.get("TOKEN")
DOMAIN = os.environ.get("DOMAIN")
HOST = os.environ.get("HOST")
TAG = os.environ.get("TAG")
ACCEPT_UNAUTHORIZED = os.environ.get("ACCEPT_UNAUTHORIZED")
def createBatch(message, n):
return '\n'.join([f"{message} {i}" for i in range(1, n + 1)])
context = (
ssl._create_unverified_context()
if ACCEPT_UNAUTHORIZED
else ssl.create_default_context()
)
conn = http.client.HTTPSConnection(URL_HOST, URL_PORT, context=context)
path = f"/stream/{DOMAIN}/token!{TOKEN}/{HOST}/{TAG}"
headers = {"Content-Type": "text/plain"}
body = createBatch("This is Sparta!", 10).encode("utf-8")
conn.request("POST", path, body=body, headers=headers)
response = conn.getresponse()
print(response.status)
print(response.read().decode())
conn.close() |
Code Block |
---|
#!/usr/bin/env bash
if [[ -z "$URL_HOST" || -z "$TOKEN" || -z "$DOMAIN" || -z "$HOST" || -z "$TAG" ]]; then
echo "Please set all required environment variables: URL_HOST, TOKEN, DOMAIN, HOST, TAG"
exit 1
fi
URL_PORT="${URL_PORT:-443}"
create_batch() {
local message="$1"
local n="$2"
local batch=""
for i in $(seq 1 $n)
do
batch+="\n$message $i"
done
printf "${batch:2}"
}
URL="https://$URL_HOST:$URL_PORT/stream/$DOMAIN/token!$TOKEN/$HOST/$TAG"
create_batch "This is Sparta!" 10 | curl -v --data-binary @- "$URL" |
Rw tab |
---|
title | Batch of events with gzip compression |
---|
|
Sending a batch of events compressed with gzip in a single HTTP request can be useful for reducing the size of the data transmitted over the network (bandwidth), especially if the events are already compressed at the source. Anatomy of the request Code Block |
---|
POST <endpoint>/stream/<domain>/token!<token>/<host>/<tag> HTTP/1.1
Host: <host>
Content-Encoding: gzip
Content-Length: <body length>
<compressed body> |
Examples Code Block |
---|
const https = require('https');
const zlib = require('zlib');
const URL_HOST = process.env.URL_HOST;
const URL_PORT = process.env.URL_PORT || 443;
const TOKEN = process.env.TOKEN;
const DOMAIN = process.env.DOMAIN;
const HOST = process.env.HOST;
const TAG = process.env.TAG;
const ACCEPT_UNAUTHORIZED = process.env.ACCEPT_UNAUTHORIZED !== undefined;
const options = {
hostname: URL_HOST,
port: URL_PORT,
path: `/stream/${DOMAIN}/token!${TOKEN}/${HOST}/${TAG}`,
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Content-Encoding': 'gzip'
},
rejectUnauthorized: !ACCEPT_UNAUTHORIZED
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', chunk => console.log(`Response: ${chunk}`));
});
req.on('error', err => console.error(err));
const batch = createBatch('This is Sparta!', 10);
const payload = zlib.gzipSync(batch);
req.end(payload);
function createBatch(message, n) {
return Array.from({length: n}, (_, i) => `${message} ${i + 1}`).join(`\n`);
} |
Code Block |
---|
import http.client
import ssl
import os
import gzip
URL_HOST = os.environ.get("URL_HOST")
URL_PORT = os.environ.get("URL_PORT", 443)
TOKEN = os.environ.get("TOKEN")
DOMAIN = os.environ.get("DOMAIN")
HOST = os.environ.get("HOST")
TAG = os.environ.get("TAG")
ACCEPT_UNAUTHORIZED = os.environ.get("ACCEPT_UNAUTHORIZED")
def createBatch(message, n):
return '\n'.join([f"{message} {i}" for i in range(1, n + 1)])
context = (
ssl._create_unverified_context()
if ACCEPT_UNAUTHORIZED
else ssl.create_default_context()
)
conn = http.client.HTTPSConnection(URL_HOST, URL_PORT, context=context)
path = f"/stream/{DOMAIN}/token!{TOKEN}/{HOST}/{TAG}"
headers = {
"Content-Type": "text/plain",
"Content-Encoding": "gzip"
}
body = createBatch("This is Sparta!", 10).encode("utf-8")
body = gzip.compress(body)
conn.request("POST", path, body=body, headers=headers)
response = conn.getresponse()
print(response.status)
print(response.read().decode())
conn.close() |
Code Block |
---|
#!/usr/bin/env bash
if [[ -z "$URL_HOST" || -z "$TOKEN" || -z "$DOMAIN" || -z "$HOST" || -z "$TAG" ]]; then
echo "Please set all required environment variables: URL_HOST, TOKEN, DOMAIN, HOST, TAG"
exit 1
fi
URL_PORT="${URL_PORT:-443}"
create_batch() {
local message="$1"
local n="$2"
local batch=""
for i in $(seq 1 $n)
do
batch+="\n$message $i"
done
printf "${batch:2}"
}
URL="https://$URL_HOST:$URL_PORT/stream/$DOMAIN/token!$TOKEN/$HOST/$TAG"
create_batch "This is Sparta!" 10 \
| gzip \
| curl -v --data-binary @- --header "Content-Encoding: gzip" "$URL"
|
Send a stream of events in a single HTTP request. Useful when the client does not know in advance the number of events to send. This method uses chunked transfer encoding to stream events using the body of the HTTP request. Anatomy of the request Code Block |
---|
POST <endpoint>/stream/<domain>/token!<token>/<host>/<tag> HTTP/1.1
Host: <host>
Transfer-Encoding: chunked
<chunk 1 size>\r\n
<chunk 1>\r\n
<chunk 2 size>\r\n
<chunk 2>\r\n
...
<chunk n size>\r\n
<chunk n>\r\n
0\r\n\r\n |
Examples Code Block |
---|
const https = require('https');
const URL_HOST = process.env.URL_HOST;
const URL_PORT = process.env.URL_PORT || 443;
const TOKEN = process.env.TOKEN;
const DOMAIN = process.env.DOMAIN;
const HOST = process.env.HOST;
const TAG = process.env.TAG;
const ACCEPT_UNAUTHORIZED = process.env.ACCEPT_UNAUTHORIZED !== undefined;
const options = {
hostname: URL_HOST,
port: URL_PORT,
path: `/stream/${DOMAIN}/token!${TOKEN}/${HOST}/${TAG}`,
method: 'POST',
headers: {
'Content-Type': 'text/plain',
'Transfer-Encoding': 'chunked'
},
rejectUnauthorized: !ACCEPT_UNAUTHORIZED
};
const req = https.request(options, res => {
console.log(`statusCode: ${res.statusCode}`);
res.on('data', chunk => console.log(`Response: ${chunk}`));
});
req.on('error', err => console.error(err));
async function main() {
const stream = createStream('This is Sparta!', 10);
for await (const message of stream) {
req.write(message);
}
req.end();
}
main();
async function* createStream(message, n, delay = 1000) {
for (let i = 1; i <= n; i++) {
yield `${message} ${i}\n`;
await new Promise(resolve => setTimeout(resolve, delay));
}
} |
Code Block |
---|
import http.client
import ssl
import time
import os
URL_HOST = os.environ.get("URL_HOST")
URL_PORT = os.environ.get("URL_PORT", 443)
TOKEN = os.environ.get("TOKEN")
DOMAIN = os.environ.get("DOMAIN")
HOST = os.environ.get("HOST")
TAG = os.environ.get("TAG")
ACCEPT_UNAUTHORIZED = os.environ.get("ACCEPT_UNAUTHORIZED")
def createChunk(message):
data = message.encode("utf-8")
size = format(len(data), 'x').encode("utf-8")
return b"%b\r\n%b\r\n" % (size, data)
def endChunk():
return b"0\r\n\r\n"
def createStream(message, n, delay=1000):
for i in range(1, n + 1):
yield f"{message} {i}\n"
time.sleep(delay / 1000)
context = (
ssl._create_unverified_context()
if ACCEPT_UNAUTHORIZED
else ssl.create_default_context()
)
conn = http.client.HTTPSConnection(URL_HOST, URL_PORT, context=context)
path = f"/stream/{DOMAIN}/token!{TOKEN}/{HOST}/{TAG}"
headers = {"Content-Type": "text/plain", "Transfer-Encoding": "chunked"}
conn.request("POST", path, headers=headers)
for message in createStream("This is Sparta!", 10):
print(message)
conn.send(createChunk(message))
conn.send(endChunk())
response = conn.getresponse()
print(response.status)
print(response.read().decode())
conn.close() |
Code Block |
---|
#!/usr/bin/env bash
if [[ -z "$URL_HOST" || -z "$TOKEN" || -z "$DOMAIN" || -z "$HOST" || -z "$TAG" ]]; then
echo "Please set all required environment variables: URL_HOST, TOKEN, DOMAIN, HOST, TAG"
exit 1
fi
URL_PORT="${URL_PORT:-443}"
create_stream() {
local message="$1"
local n="$2"
for i in $(seq 1 $n)
do
printf "$message $i\n"
sleep 1
done
}
URL="https://$URL_HOST:$URL_PORT/stream/$DOMAIN/token!$TOKEN/$HOST/$TAG"
# It appears that curl buffers the chunks and sends them once all messages
# are generated as a single chunk. This behavior likely occurs because
# curl has an internal buffer, and when all messages are combined, they
# do not exceed this buffer limit.
create_stream "This is Sparta!" 10 \
| curl -v --data-binary @- --header "Transfer-Encoding: chunked" "$URL" |
|