Skip to main content
October 21, 2022
Question

Web Api to download document using document id

  • October 21, 2022
  • 3 replies
  • 0 views

Hi,

I have a scenario where i need to download document using web api.

All the document id's stored in local db and uploaded documents are stored in document center.

Now i need a web api to download document that's residing inside in appian.

The default document download link works only inside appian.

i have used an expression rule inside web api where gives me the list of documents needed but download document from api gives only the document id. But unable to document using web api.

Can someone help me on this?

Thanks in advance!

3 replies

stefanhelzle0001
October 21, 2022

There is a ready made pattern available when you create a new Web API. The resulting code is this:

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)
    )
  )
)

April 23, 2024

Hi [mention:a11f77a729fb4db2af76b09948583328:e9ed411860ed4f2ba0265705b8793d05] i have apply all this things but how we can pass document id or document path in the web api .

stefanhelzle0001
April 23, 2024

Not sure what you mean, this is from the Appian example and the API expects a valid document ID in the URL.