Skip to main content
February 27, 2021
Question

Document to Base64 expose as web-api

  • February 27, 2021
  • 3 replies
  • 0 views

I have requirement where the web-api needs to be exposed which convert the appian document into base64 based on local id of document received in the request.

Appian version :20.4(on premise)

3 replies

March 8, 2021

you mean like so?

a!localVariables(
  local!pathArray: fn!cast('type!{http://www.appian.com/ae/types/2009}Text?list', http!request.pathSegments),
  local!document: tointeger(index(local!pathArray, 1, null)),
  /* We don't want to serve arbitrary documents because it could be a security risk */
  local!extensionWhitelist: {"pdf", "txt", "png", "jpg", "jpeg"},
  if(
    /*
    * The path must be only 1 value, the document ID, and that ID must be a number.
    * Otherwise, we return a 404 Not Found
    */
    or(
      length(local!pathArray) <> 1,
      isnull(local!document),
      not(contains(local!extensionWhitelist, document(local!document, "extension")))
    ),
    a!httpResponse(
      statusCode: 404,
      body: "404 Not Found",
      headers: {}
    ),
    a!httpResponse(
      /*
      * If the query parameter "attachment" is set to true,
      * set an HTTP header that tells the client that the body of the response
      * should be downloaded. This overrides the default content-disposition
      * of "inline; filename=.". We will automatically
      * set the content-type and length.
      */
      headers: if(
        http!request.queryParameters.attachment,
        a!httpHeader(
          name: "Content-Disposition",
          value: concat(
            "attachment; filename=""",
            document(local!document, "name"),
            ".",
            document(local!document, "extension"),
            """"
          )
        ),
        {}
      ),
      body: todocument(local!document)
    )
  )
)

aravind.sAuthor
March 8, 2021

Ahmad,

I hope this retrieve the document in Binary format not in base 64...

March 8, 2021
document into base64

you asked to retrieve a document in base64, that is how appian sends documents by default