To communicate between services (or even within your own services hosted on Routzie), use the fetch
function. This function allows you to easily send HTTP requests and automatically handles various data types for both input and output.
Fetch automatically detects the data type:
BlobReference
, it will be sent as a blob with the correct MIME type.FormData
.The same logic applies to the response:
response.body
will be a BlobReference
.response.body
will be an object.response.body
will be a string.route('GET', 'download-image', async function downloadImage(req: ProcessedRequest) { const result = await fetch(req.context, 'https://picsum.photos/200/300') if (result.body instanceof BlobReference) { return new RouteResult(result.body, 200) } return new RouteResult('Unable to resolve image', 500) })
route('POST', 'send-data', async function sendData(req: ProcessedRequest) { const payload = { name: 'Test', value: 42 } const result = await fetch(req.context, 'http://otherMachine/api/receive', { method: 'POST', body: payload }) if (typeof result.body === 'object') { return new RouteResult(result.body, 200) } return new RouteResult('Failed to send data', 500) })
route('POST', 'upload-file', async function uploadFile(req: ProcessedRequest) { const fileBlob = req.body.file // assuming BlobReference const result = await fetch(req.context, 'http://localhost/storage/upload', { method: 'POST', body: fileBlob }) if (result.body instanceof BlobReference) { return new RouteResult('File uploaded', 200) } return new RouteResult('Upload failed', 500) })
route('POST', 'submit-form', async function submitForm(req: ProcessedRequest) { const form = new FormData() form.append('username', 'user1') form.append('file', req.body.file) const result = await fetch(req.context, 'http://localhost/form', { method: 'POST', body: form }) return new RouteResult(result.body, 200) })
result.body
according to the expected output.BlobReference
for binary data.© 2025 Routzie Routzie.com