Edge Functions

Handling Compressed Requests

Handling Gzip compressed requests.


To decompress Gzip bodies, you can use gunzipSync from the node:zlib API to decompress and then read the body.

1
import { } from 'node:zlib'
2
3
.(async () => {
4
try {
5
// Check if the request body is gzip compressed
6
const = ..('content-encoding')
7
if ( !== 'gzip') {
8
return new ('Request body is not gzip compressed', {
9
: 400,
10
})
11
}
12
13
// Read the compressed body
14
const = await .()
15
16
// Decompress the body
17
const = (new ())
18
19
// Convert the decompressed body to a string
20
const = new ().()
21
const = .()
22
23
// Process the decompressed body as needed
24
.(`Received: ${.()}`)
25
26
return new ('ok', {
27
: { 'Content-Type': 'text/plain' },
28
})
29
} catch () {
30
.('Error:', )
31
return new ('Error processing request', { : 500 })
32
}
33
})