Skip to content

QueryParamsEncoding

Mihael Safaric edited this page Nov 20, 2019 · 1 revision

HttpClient encodes query parameters by default. Encoding strategy is specified in a HttpParams class. If the default encoding doesn't suit your needs, you can specify a custom encoder on a datastore level which will be used when in HttpParams.

You can either create a new class which implements HttpParameterCodec or you can extend HttpUrlEncodingCodec class.

Example:

class CustomEncoder implements HttpParameterCodec {
  encodeKey(key: string): string {
    return encodeURIComponent(key);
  }

  encodeValue(value: string): string {
    return encodeURIComponent(value);
  }

  decodeKey(key: string): string {
    return decodeURIComponent(key);
  }

  decodeValue(value: string): string {
    return decodeURIComponent(value);
  }
}

Datastore:

@DatastoreConfig({
  ...
})
export class MyDatastoreService extends DatastoreService {
  protected httpParamsOptions = {
    encoder: new CustomEncoder(),
  };
}