Document to Base64 expose as web-api

Certified Lead Developer

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)

  Discussion posts and replies are publicly visible

Parents
  • 0
    Certified Senior Developer

    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=<filename>.<extension>". 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)
        )
      )
    )

Reply
  • 0
    Certified Senior Developer

    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=<filename>.<extension>". 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)
        )
      )
    )

Children