Client interoperability
Table of contents
This page describes methods to achieve interoperability with some well-known clients.
Annotated services
Axios
Axios works with Armeria annotated services without issues, except
when serializing arrays as query parameters. By default, arrays are serialized as ?foo[]=1&foo[]=2
,
while Armeria expects ?foo=1&foo=2
.
This behavior can be modified by specifying a custom paramsSerializer
function when creating a new
Axios instance. For example, you can use the qs library:
const axios = require('axios');
const qs = require('qs');
function paramsSerializer(params) {
return qs.stringify(params, { arrayFormat: 'repeat' });
}
const client = axios.create({ paramsSerializer });
If you prefer not to add an extra dependency, you can use the following function instead:
function paramsSerializer(params) {
const parts = [];
for (const [key, values] of Object.entries(params)) {
if (values === null || typeof values === 'undefined') {
continue;
}
for (const value of [values].flat()) {
parts.push(`${key}=${encodeURIComponent(value)}`);
}
}
return parts.join('&');
}
The implementation is based on the default serializer in Axios.