Layer7 API Management

 View Only
  • 1.  Sending Email Assertion with Attachment

    Posted Jun 03, 2021 09:36 AM
    We got a json payload with multiple attachment in base 64 format

    example:
    {
    "messages": [ {
    "channel": "email",
    "destination": ["someuser@email.co.za"],
    "clientMessageId":"123",
    "message": {
    "payload_type":"html",
    "content": "samplebase64encoded content",
    "subject": "Sample subject",
    "attachment":[
    {
    "content": "base64encodedFile",
    "name": "Test",
    "contentType":"image/jpg",
    "sha256Hash": "f454d98fc28822193d9b486ce52aa8ddde68db088c49e31359d49150b22b9677"

    }
    ]
    }
    }]
    }

    The attachment can be multiple any suggestion on how to add
    all the attachment as we cannot know the number of attachments


    ------------------------------
    Pre-Sales Consultant
    CA Southern Africa
    ------------------------------


  • 2.  RE: Sending Email Assertion with Attachment
    Best Answer

    Posted Jun 05, 2021 06:56 PM
    Hello Ronald,

    We had run into same problem recently and I was able to find a way to get this working.

    You need to build a MIME message with multiple attachments and then feed it to "send email assertion"

    Below are the steps.

    1. Evaluate the json path expression to get the attachment array from the request.

    2. Add run assertion for each item to iterate through each attachment.

    3. Inside the loop construct the MIME message like below


    Each iteration will build this MIME message

    4. Close the MIME message using another set context variable assertion as below


    5. Feed the final variable to the send email assertion


    This will send the multiple attachments via email.

    Please let me know if you need any further details.

    Thanks,
    Adarsh


  • 3.  RE: Sending Email Assertion with Attachment

    Posted Jun 10, 2021 05:19 AM

    I have an issue with looping trough an rest with base64 encoded REST with attachments.

    My issue is not constructing the MIME message but the format of the attachment. The attachment gets sent but the file is corrupt.

    I tried do base64 decoding into string, into attachment of different content types. Even tried different encoding types e.g. UTF-8 and Windows-1252.

    Even tried forcing the content type to what I send in via REST



    ------------------------------
    API Tech Lead
    Sanlam
    ------------------------------



  • 4.  RE: Sending Email Assertion with Attachment

    Posted Jun 11, 2021 03:31 AM

    I figured this out.

    By setting the Content-Transfer-Encoding: base64 you can use the base64 encoded string as is and not try and decode it.

    --XXXXboundary
    Content-Description: File Attachment
    Content-Disposition: attachment;filename="${attachmentName.result}"
    Content-Type: ${attachmentContentType.result}
    Content-Transfer-Encoding: base64

    Using octet-stream also works

    Content-Type: application/octet-stream

    If you need to do document type filtering you can actually extract the content type from the file

    Note the character encoding when you decode


    Convert the content to hex

    Then just capture a few packets from the start of the file

    You can now use those headers to validate the file type. e.g.



    ------------------------------
    API Tech Lead
    Sanlam
    ------------------------------