API Connect

 View Only
Expand all | Collapse all

Extracting File Name with Extension from incoming request (multipart/form data)

  • 1.  Extracting File Name with Extension from incoming request (multipart/form data)

    Posted Thu February 02, 2023 06:36 AM
    Edited by Stefen Salvatore Fri February 03, 2023 02:51 AM

    Hi @Steve Linn,
    I've a requirement like I need to pick the name of incoming "form-data" file including the extension and file size using Gatewayscript in APIC V10.
    for example : "test.csv". and need to 'block'  file extensions Otherthan "csv" ' and if the file size is more than 4mb then also it should reject the session. Iam trying this from past few days but iam unable to extract filename from Content-Disposition and Size of the file from form-data
    Treat this as priority 1 and help me in achieving this.


    Thanks in Advance!!!



    ------------------------------
    Vyasavardhan Ramagiri
    ------------------------------​


  • 2.  RE: Extracting File Name with Extension from incoming request (multipart/form data)

    Posted Tue April 04, 2023 05:25 AM

    Hi,

    I found this enhancement idea.

    https://integration-development.ideas.ibm.com/ideas/APICONN-I-602

    It requests supporting multipart/form-data in API Connect. The status of the idea is "Future consideration."

    I was wondering if you could consider to vote for the idea and/or to add your comment.

    SAKAIRI Takashi

    IBM Japan



    ------------------------------
    TAKASHI SAKAIRI
    Cloud Integration Architect - WW SWAT, Integration and Development
    IBM Japan
    ------------------------------



  • 3.  RE: Extracting File Name with Extension from incoming request (multipart/form data)

    Posted Tue April 04, 2023 04:16 PM

    Hi Vyasavardhan and Takashi,

    Note in 10.5.0.3 of the DataPower Gateway (which I believe would require a similar 10.0.5 version of API Connect), although I've not tried this it appears support was added which I believe is what you seek.  See https://www.ibm.com/docs/en/datapower-gateway/10.5?topic=object-manipulating-attachments-in-message.  Although this documentation is in the DataPower documentation, it is under a section context object for the API gateway.  The parse policy will parse the inbound message into various parts.  Slightly modifying one of the examples from the documentation:

    for (var i = 0; i < context.message.attachments.count; i++) {
      let contentDisposition = context.message.attachments[i].header.get('Content-Disposition');
      // examine the content-disposition header here, specifically the filename attribute
      // and reject the transaction if the filename isn't of a .csv extension, also do a break in case there
      // are more attachments to be inspected as I don't believe context.reject actually terminates your
      // code.
    }

    Typically headers in each attachment are of the Content-Disposition and Content-Type, something like:

    Content-Disposition: form-data; name="somename"; filename="file.txt"
    Content-Type: text/plain

    So this would allow you within that for loop to extract the file name of each attachment from the Content-Disposition header and then examine the extension to see if it is not a .csv and then to issue a context.reject in that use case to fail the transaction.

    All new so you may be on the bleeding edge of trying this out.  I'd be interested in how it works for you.

    Best Regards,

    Steve



    ------------------------------
    Steve Linn
    Senior Consulting I/T Specialist
    IBM
    ------------------------------