Search code examples
xmlhttprequestloopback

How can I have a text request body in Loopback?


I have an api endpoint which I'd like to post text to as the body.

I've tried this code:

@post('/my-endpoint', {
    responses: {}
  })
  async createFromCsv(
    @requestBody({content: {'application/text': {}}}) csv: string,
){
  // code
}

but get an error:

UnsupportedMediaTypeError: Content-type application/text is not supported.

I kind of pieced together the code above from the comments I found here: https://github.com/strongloop/loopback-next/blob/8ae8a0a81db205f052b81caaceece5303cd80ff2/packages/openapi-v3/src/decorators/request-body.decorator.ts#L68

How can I have a text request body?


Solution

  • Turns out the code I picked out of the comments wasn't totally sensible.

    The proper content type for text is text/plain.

    e.g.

    @requestBody({content: {'text/plain': {}}}) csv: string
    

    There is also text/csv and text/html.

    More info on MIME types here (https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types)