Layer7 API Management

Expand all | Collapse all

Handling MIME Message with Attachments in REST API

  • 1.  Handling MIME Message with Attachments in REST API

    Posted 09-17-2016 05:51 AM

    Hi All,

     

    Currently we are trying to implement REST API that has to accept MIME message with attachments also. Is it possible with REST API.If yes , please provide sample implementation code for reference . 

     

    And also I have query , If possible to send MIME message with attachments to REST API then how to get message in API for further process.Tried to get message in API with ${request.mainpart} but it not getting anything for further validation on some input data.

     

    Thanks,

    Suneel



  • 2.  Re: Handling MIME Message with Attachments in REST API

    Posted 09-17-2016 10:49 PM

    Hi Sai Suneel,

     

    Yes, you can send attachments using REST API's and you don't have to do anything extra in the policy rather than just sending the file in form-data along with other fields (if any)

     

    Reading the file upload data will give binary data, other than text file. Not sure why you wanted to read the data as gateway is used to send the file to some external server while will be processed or stored.

     

    Thank You

    Sampath Kumar



  • 3.  Re: Handling MIME Message with Attachments in REST API

    Posted 09-18-2016 05:02 AM

    Hi Sampath,

     

    Thanks for the response.

     

    Based on request field I want to route request to respective backend system.Hence I am trying to compare request element.

     

    Could you please provide any code for reference to my understanding.

     

    Thanks in advance.

     

    Suneel



  • 4.  Re: Handling MIME Message with Attachments in REST API

    Posted 09-19-2016 03:16 AM

    Hi Saisuneel,

                 Where is your request field located? It will be good idea to have a multi-part request. You can add your request field as a multi-part header. the list of variables can be found Message Layer Context Variables - CA API Gateway - 9.1 - CA Technologies Documentation.

     

    Thanks,

    Abhishek



  • 5.  Re: Handling MIME Message with Attachments in REST API

    Posted 09-20-2016 06:36 AM

    Hi Sampath,

    Without performing validation on MIME message(with attachments) can we send client request to back-end ?. If yes, how can I send it by using ${request.parts.1.body} or else any other way is there.I want to send message with attachments to back-end.

     

    Thanks,

    Suneel



  • 6.  Re: Handling MIME Message with Attachments in REST API

    Posted 09-19-2016 01:47 PM

    Hi Abhishek,

    Request field located in request body.I have used to get message with ${request.originalmainpart} but it is not giving message. But where as I used ${request.parts.1.body} then it gave request body message.

     

    Can we send mime message and attachments to back end system that has sent by client ?.

     

    I have tried to get MIME message with attachments in REST in gateway but not getting clear understanding on it.If CA Team can help me on it then it would be grateful.


    Thanks,
    Suneel

     



  • 7.  Re: Handling MIME Message with Attachments in REST API

    Posted 09-21-2016 11:26 PM

    Hi saisuneel,

    When you get x part of the MIME message, you can use ${<MIME message>.parts.x.body} for text, and ${<MIME message>.parts.x} for binary.

     

    When you want to construct a MIME message (to send to back end),

    1. message Content-Type:

    multipart/mixed;boundary=XXXXboundary

     

    2. message body:

    --XXXXboundary
    Content-Type: text/plain

    this is the body text

    --XXXXboundary
    Content-Description: File Transfer
    Content-Disposition: attachment;filename="test.txt"
    Content-Type: application/octet-stream
    Content-Transfer-Encoding: binary
    Expires: 0
    Cache-Control: must-revalidate, post-check=0, pre-check=0
    Pragma: public

    ${attachment}

    --XXXXboundary--



  • 8.  Re: Handling MIME Message with Attachments in REST API

    Posted 09-22-2016 07:01 AM

    Hi Zhijun He,

     

    Yes , I am able to get mime message body parts individually when I used ${<MIME message>.parts.x.body}. If it is possible to get entire mime message that has sent from the client i.e. with headers with bodies.

     

    I am trying to send Mime message with attachments from client to gateway REST API, then I really want to know  sending request is reaching to back-end or not ? so that is there any way to see whole request.

     

    Thanks,

    Suneel



  • 9.  Re: Handling MIME Message with Attachments in REST API

    Posted 09-22-2016 08:38 PM

    Hi saisuneel,

    You can run a loop on message.parts to get all the parts. some thing like this,

    <?xml version="1.0" encoding="UTF-8"?>
    <wsp:Policy xmlns:L7p="http://www.layer7tech.com/ws/policy" xmlns:wsp="http://schemas.xmlsoap.org/ws/2002/12/policy">
    <wsp:All wsp:Usage="Required">
    <L7p:SetVariable>
    <L7p:Base64Expression stringValue=""/>
    <L7p:VariableToSet stringValue="allparts"/>
    </L7p:SetVariable>
    <L7p:ForEachLoop L7p:Usage="Required"
    loopVariable="response.parts" variablePrefix="parts">
    <L7p:AuditDetailAssertion>
    <L7p:Detail stringValue="${parts.current.body}"/>
    </L7p:AuditDetailAssertion>
    <L7p:SetVariable>
    <L7p:Base64Expression stringValue="JHthbGxwYXJ0c30NCi0tLS0tLS0tLS0tDQoke3BhcnRzLmN1cnJlbnQuYm9keX0="/>
    <L7p:VariableToSet stringValue="allparts"/>
    </L7p:SetVariable>
    </L7p:ForEachLoop>
    <L7p:AuditDetailAssertion>
    <L7p:Detail stringValue="${allparts}"/>
    </L7p:AuditDetailAssertion>
    </wsp:All>
    </wsp:Policy>

     

    The example only get the body. For header or else, please refer to the document Abhishek provided.

     

    After you prepare a mime message variable, you send it via Route via HTTP(S) assertion, if the assertion doesn't throw any error, then it must reach the backend. You may use tcpdump to capture the outbound request.

     

    Regards,

    Mark