Skip to content

Request body Composers

Tambapps edited this page Jun 3, 2022 · 5 revisions

Composing request bodies

Composers allow you to convert data into a request body. It is basically a function that takes in argument an Object (the object provided through the body parameter) and returns one of the following objects:

  • a string
  • a byte array
  • an InputStream
  • a okhttp3.RequestBody

You can find all the default composers in the Composers class. The poet holds composers by ContentType (the chosen composer will depend on the ContentType of the request). You can consult, edit/ovrride default composers like in this example

poet.composers[ContentType.HTML] = String.&valueOf

poet.composers[new ContentType("image/jpeg")] = myCustomComposer

// or change composer for a specific call
poet.post('/something', composer: customComposer, body: myBody)

Multipart request body

The MULTIPART_FORM allows you to perform multipart requests. When using it, you must pass a map of String -> FormData (part key -> part).

The form part has the following properties

  • value -> the value that will be composed
  • contentType -> the content type of this part (will be used to know which composer to use when composing FormData's value
  • filename (optional) -> the filename of this part, defaults to the part's key

Examples

poet.post("/upload-file", contentType: ContentType.MULTIPART_FORM,
    body: [file: new FormData(value: new File('some.file'), contentType: ContentType.BINARY)])

Clone this wiki locally