Web Api to download document using document id

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!

  Discussion posts and replies are publicly visible

  • 0
    Certified Lead Developer

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

  • 0
    Certified Associate Developer
    in reply to Stefan Helzle

    Hi  i have apply all this things but how we can pass document id or document path in the web api .

  • 0
    Certified Lead Developer
    in reply to chiragj3013

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