Skip to content

Body formats ​

Send and receive JSON, form, text and binary bodies, or add your own format.

The examples use the PaymentsClient from SDK types in your models. Its createPayment operation sends a payment as JSON. money is { amount, currency } and amount is a decimal string.

web/src/submit-payment.ts:

ts
const response = await client.createPayment().execute({ body: draft });

Validation checks the request value and the decoded response value. Codecs run even when validation is off.

Built-in formats ​

Declared mediaMatchesRequestResponse
JSON*json*Plain data as JSON textJSON text as plain data
Texttext/* except XML, YAML and SSEStringString
URL-encoded formapplication/x-www-form-urlencodedDeclared form fieldsNeeds a responseDecoders entry
Multipart formmultipart/form-dataDeclared form fieldsNeeds a responseDecoders entry
Binary, such as PDF or ZIPimage/*, audio/*, video/*, font/*, PDF, ZIP, gzip, octet-streamBlobBlob
Empty responseNo content declaredNoneNo body
  • The OpenAPI contract sets the argument and return types.
  • For a Blob response, call arrayBuffer() or text() when you need the bytes or the text.
  • Let fetch set the multipart boundary. An encoder that returns FormData must omit contentType.
  • A form with advanced encoding settings in the document needs a bodyEncoders entry.
  • A response of another media type, such as XML or YAML, needs a responseDecoders entry.
  • A request body of another media type must be a string, Blob, FormData or URLSearchParams. Otherwise it needs a bodyEncoders entry.

Add a custom format ​

Suppose the service sends money as text such as USD 25.00. Declare application/vnd.provider.money for the request and the response, with this decoded schema:

yaml
schema:
  type: object
  required: [amount, currency]
  properties:
    amount: { type: string, pattern: '^[0-9]+(?:\\.[0-9]{1,2})?$' }
    currency: { type: string, pattern: '^[A-Z]{3}$' }

Declare it on a createChargeText operation and generate ProviderClient again. The provider's Money schema has the same fields.

provider/client.ts:

ts
export function createMoneyTextClient(baseUrl: string, token: string): ProviderClient {
  return new ProviderClient({
    baseUrl,
    auth: { bearerAuth: token },
    bodyEncoders: {
      'application/vnd.provider.money': (value, { contentType }) => {
        if (typeof value !== 'object' || value === null
          || !('currency' in value) || !('amount' in value)) {
          throw new TypeError('Expected money fields');
        }
        return { body: `${value.currency} ${value.amount}`, contentType }; // { amount: '25.00', currency: 'USD' } → 'USD 25.00'
      },
    },
    responseDecoders: {
      'application/vnd.provider.money': async response => {
        const match = /^([A-Z]{3}) ([0-9]+(?:\.[0-9]{1,2})?)$/.exec(await response.text());
        if (!match) throw new TypeError('Expected money text, such as USD 25.00');
        return { currency: match[1], amount: match[2] }; // Response validation checks this object.
      },
    },
  });
}

Codec options ​

OptionInputReturn value
bodyEncodersThe request value (unknown) and a context with contentType and the encoding rulesBodyInit, or { body, contentType? }
responseDecodersThe ResponseThe decoded value, or a promise of it
  • Each option is an object keyed by media type. For bodyEncoders, the client tries the declared request media type, then the type and subtype in lowercase without parameters, then */*.
  • For responseDecoders, the client tries the actual response content type, then the declared one, then the lowercase type and subtype, then */*.
  • A configured codec replaces the built-in handling.
  • A decoder must return the shape that the response schema declares.
  • A missing required codec, an encoding failure or a decoding failure rejects .execute().
  • If your decoder calls response.body.getReader(), call reader.releaseLock() when it ends or fails. The client can then cancel the unread body.

A text body whose schema allows a type other than string (including nullable) needs a codec. Without one, .execute() rejects and names the missing option.

A response that sends items one at a time uses stream decoders instead.