Document Upload WEB API Returning Success on empty body

Certified Associate Developer

Hello,

I have a handler for document upload WEB API that has validation if the binary body is empty. The problem is in Appian, the validation is working but in Postman, it returns success even when the body is empty.

a!localVariables(
  local!params: property(ri!request, "queryParameters", null),
  local!body: property(ri!request, "body", null),
  local!fileName: property(local!params, "filename", null),
  local!transactionId: property(local!params, "transactionid", null),
  local!docCategoryId: property(local!params, "doccategoryid", null),
  local!docTypeId: property(local!params, "documenttypeid", null),
  local!validation: reject(
    fn!isnull,
    {
      if(
        or(
          a!isNullOrEmpty(local!transactionId),
          a!isNullOrEmpty(local!docCategoryId),
        ),
        "Missing Parameters. Please Try Again",
        if(
          or(
            a!isNotNullOrEmpty(local!docTypeId),
            a!isNotNullOrEmpty(local!fileName)
          ),
          if(
            a!isNullOrEmpty(local!body),
            "Missing Body. Please try again.",
            null
          ),
          null
        )
      )
    }
  ),
  if(
    length(local!validation) > 0,
    rule!REST_JsonResponse(
      statusCode: 400,
      body: {
        title: "Missing Parameters Error",
        message: local!validation[1],
        code: cons!CSB_COMMONS_ERROR_CODE_MISSING_PARAMETERS
      }
    ),
    a!startProcess(
      processModel: cons!RMS_UploadDocument_PM,
      processParameters: {
        fileName: tostring(local!fileName),
        document: local!body,
        transactionId: tointeger(local!transactionId),
        documentType: tointeger(local!docTypeId),
        docCategory: tointeger(local!docCategoryId),
        
      },
      onSuccess: if(
        or(
          a!isNullOrEmpty(local!docTypeId),
          a!isNullOrEmpty(local!body)
        ),
        rule!REST_JsonResponse(
          statusCode: 200,
          body: {
            transactionId: local!transactionId,
            documentCategory: fv!processInfo.pv.documentCategoryStatus_CDT.category,
            documentTypes: a!forEach(
              items: fv!processInfo.pv.documentTypeUnderCategoryTransaction_CDT,
              expression: fv!item.documentTypeId
            )
          }
        ),
        rule!REST_JsonResponse(
          statusCode: 200,
          body: {
            transactionId: local!transactionId,
            documentId: fv!processInfo.pv.document_CDT.id,
            fileName: fv!processInfo.pv.document_CDT.fileName,
            fileExtension: fv!processInfo.pv.document_CDT.fileExtension,
            createdDate: fv!processInfo.pv.document_CDT.createdDate,
            approvalDate: fv!processInfo.pv.document_CDT.approvalDate,
            expirationDate: fv!processInfo.pv.document_CDT.expirationDate,
            documentType: fv!processInfo.pv.document_CDT.documentType.documentTypeId.documentType,
            documentCategory: fv!processInfo.pv.document_CDT.documentType.categoryId.category.category,
            filePath: fv!processInfo.pv.document_CDT.filePath,
          }
        )
      ),
      onError: rule!REST_JsonResponse(
        statusCode: 500,
        body: {
          title: "Internal Server Error",
          message: "Unknown error occured. Please contact the Administrator",
          code: cons!CSB_COMMONS_ERROR_CODE_UNKNOWN_ERROR
        }
      )
    )
  )
)

As you can see, it still returns status 200

But in Appian, the validation is working

This also happens even in the default expression of the Document Upload web API.

  Discussion posts and replies are publicly visible

Parents Reply
  • 0
    Certified Associate Developer
    in reply to Rómulo

    Yes, that is because you did not input params as well, returning the 1st validation error in the condition which is "Missing Parameters". The problem is you must have a body when you input query params. But in my code, it still returns status 200 even if I didn't input a body in postman. This only happens in postman and other external systems as the validation is working in Appian.

Children
  • Hi ,

    Yes, I got you. See, what is happening is that Appian is pre-uploading empty document when it is called by external systems, so when you try to evaluate a!isNullOrEmpty(local!body) it does not work due to is never empty. This action never happens when you test it directly with Appian, there is the difference. Therefore, you can evaluate the size of the document instead of it exists. I've just tried it is fully working, please do the following in order to correct your problem:

    Solution



    Problem Evidence







    Hope it helps,

    Rómulo