How to make Request Body accept empty body as well as JSON?

Hi everyone

Does anyone know how to make the request body optional in RAML specs?

We want to be able to use PATCH on the same endpoint for two different cases:

  • queryParams supplied, with empty body
  • no queryParams, with JSON body

However, our error handling kit keeps catching the first case with the empty body as 415 Unsupported Media Type. Has anyone ever had to do this before?

Thanks
Desy

@dkristianti-dtt: Welcome! Are you trying to make the body schema conditionally enforceable depending on the presence or not of queryParams? Would you mind sharing an example of the RAML you have written so far?

I want to reuse the same endpoint for different kinds of mediatypes (one being JSON body, one being an empty body).

The RAML I have so far looks something like the following snippet:

#%RAML 1.0
/accounts:
  patch:
    body:
      application/json:
        type: account | nil

The full RAML has the query parameters added as traits.

However whenever I want to pass an empty body I still need to define the content type as json, otherwise it’s caught as 415 Unsupported Media Type…

I think the real problem here is that RAML doesn’t seem to allow specifying both a content entity in the payload and no content entity.

Using MIME types as the keys under body: doesn’t really allow a way to say “no content entity”. This is… unfortunate.

application/json does not accept empty bodies. It requires at least null (the string null) which would validate against the type: nil. See RFC7159.

application/octet-stream however does accept empty bodies so you could have a second media type definition under your body which would have type: nil defined and instruct your API consumers to use that content type when sending empty bodies, that’s the proper way anyway.

Not setting a media types is not an option – even if it was one in RAML – most proxy, clients and implementations would choke on not getting a Content-Type header.