Topic
  • 9 replies
  • Latest Post - ‏2019-08-23T07:09:13Z by Sudarshan Bandaru
tmsaaki
tmsaaki
18 Posts

Pinned topic Multipart/form-data using GatewayScript

‏2019-07-04T17:14:43Z |

I have requirement where i need to send form-data to the backend. Is there any way i can send form data to backend using Gateway Script. 

 

I am using multi protocol gateway. 

 

Thanks in advance. 

  • HermannSW
    HermannSW
    8692 Posts
    ACCEPTED ANSWER

    Re: Multipart/form-data using GatewayScript

    ‏2019-07-04T17:36:43Z  

    Just generate the data you want to send by accumulating into a string variable and then use urlopen module:

    var str="";
    var boundary="________someBoundary";
    str += boundary + "\n";
    str += "Content-Disposition: form-data; name=\"someFile\"; filename=\"foo.bar\"\n";
    str += "Content-Type: text/plain\n";
    ....
    str += boundary + "--\n";
    ...
    

     

    Hermann.

    Updated on 2019-07-04T17:38:16Z at 2019-07-04T17:38:16Z by HermannSW
  • HermannSW
    HermannSW
    8692 Posts

    Re: Multipart/form-data using GatewayScript

    ‏2019-07-04T17:36:43Z  

    Just generate the data you want to send by accumulating into a string variable and then use urlopen module:

    var str="";
    var boundary="________someBoundary";
    str += boundary + "\n";
    str += "Content-Disposition: form-data; name=\"someFile\"; filename=\"foo.bar\"\n";
    str += "Content-Type: text/plain\n";
    ....
    str += boundary + "--\n";
    ...
    

     

    Hermann.

    Updated on 2019-07-04T17:38:16Z at 2019-07-04T17:38:16Z by HermannSW
  • tmsaaki
    tmsaaki
    18 Posts

    Re: Multipart/form-data using GatewayScript

    ‏2019-07-05T06:11:10Z  

    Thank you Hermann. It is working for me. 

  • Sudarshan Bandaru
    Sudarshan Bandaru
    92 Posts

    Re: Multipart/form-data using GatewayScript

    ‏2019-08-20T17:33:33Z  
    • tmsaaki
    • ‏2019-07-05T06:11:10Z

    Thank you Hermann. It is working for me. 

    Hi Herman,

     

    I have similar requirement where I need to send csv file by using Multipart/form-data. I am using the below code snippet, but the backend system is unable to recognise the content for some reason.

     

    var str="";
    var boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW";
    str += boundary + "\n";
    str += "Content-Disposition: form-data; name=\"file\"; filename=\"details.csv\"\n";
    str += "Content-Type: application/vnd.ms-excel\n";
    str += "Name,Company\n";
    str += "Priyanka,Icon\n";
    str += boundary + "--\n";
    session.output.write(str);

     

    I am also setting the content type in the headers as below, but still no luck.

    multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

     

  • HermannSW
    HermannSW
    8692 Posts

    Re: Multipart/form-data using GatewayScript

    ‏2019-08-21T08:37:48Z  

    Hi Herman,

     

    I have similar requirement where I need to send csv file by using Multipart/form-data. I am using the below code snippet, but the backend system is unable to recognise the content for some reason.

     

    var str="";
    var boundary="----WebKitFormBoundary7MA4YWxkTrZu0gW";
    str += boundary + "\n";
    str += "Content-Disposition: form-data; name=\"file\"; filename=\"details.csv\"\n";
    str += "Content-Type: application/vnd.ms-excel\n";
    str += "Name,Company\n";
    str += "Priyanka,Icon\n";
    str += boundary + "--\n";
    session.output.write(str);

     

    I am also setting the content type in the headers as below, but still no luck.

    multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

     

    Each header like "Content-Disposition" has to be followed by CRLF according spec:
    https://tools.ietf.org/html/rfc2045#section-3


    And the body part needs an additional CRLF after the headers:

    body-part := MIME-part-headers [CRLF *OCTET]
    

    https://tools.ietf.org/html/rfc2046#section-5.1.1

     

    So this modified lines should make it work:

    str += boundary + "\n";
    str += "Content-Disposition: form-data; name=\"file\"; filename=\"details.csv\"\r\n";
    str += "Content-Type: application/vnd.ms-excel\r\n";
    str += "\r\n";
    str += "Name,Company\n";
    

     

    Hermann,

     

    P.S:
    dash-boundary needs to have CRLF as well, please look at spec.

    Updated on 2019-08-21T08:40:53Z at 2019-08-21T08:40:53Z by HermannSW
  • Sudarshan Bandaru
    Sudarshan Bandaru
    92 Posts

    Re: Multipart/form-data using GatewayScript

    ‏2019-08-22T01:53:24Z  
    • HermannSW
    • ‏2019-08-21T08:37:48Z

    Each header like "Content-Disposition" has to be followed by CRLF according spec:
    https://tools.ietf.org/html/rfc2045#section-3


    And the body part needs an additional CRLF after the headers:

    <pre class="javascript dw" dir="ltr">body-part := MIME-part-headers [CRLF *OCTET] </pre>

    https://tools.ietf.org/html/rfc2046#section-5.1.1

     

    So this modified lines should make it work:

    <pre class="javascript dw" dir="ltr">str += boundary + "\n"; str += "Content-Disposition: form-data; name=\"file\"; filename=\"details.csv\"\r\n"; str += "Content-Type: application/vnd.ms-excel\r\n"; str += "\r\n"; str += "Name,Company\n"; </pre>

     

    Hermann,

     

    P.S:
    dash-boundary needs to have CRLF as well, please look at spec.

    Thanks Herman.

    I tried adding CRLF, but no luck. 

     

    I have analyzed this further, I have tested this from Postman and captured packets. When I see the HTTP content I can clearly see First boundary, Encapsulated multipart headers etc from the postman request. I have attached the screenshot(postman) and the packet capture(refer to row number 18) for your reference.

    But when I the mimic same message using Gateway script, I see everything going under "Data" header, I don't see them like how the Postman tool is sending. Please see the screenshot(Gateway), refer the row number #22 in packet capture and please advise what could be the issue. Thanks.

     

  • HermannSW
    HermannSW
    8692 Posts

    Re: Multipart/form-data using GatewayScript

    ‏2019-08-22T15:25:41Z  

    Thanks Herman.

    I tried adding CRLF, but no luck. 

     

    I have analyzed this further, I have tested this from Postman and captured packets. When I see the HTTP content I can clearly see First boundary, Encapsulated multipart headers etc from the postman request. I have attached the screenshot(postman) and the packet capture(refer to row number 18) for your reference.

    But when I the mimic same message using Gateway script, I see everything going under "Data" header, I don't see them like how the Postman tool is sending. Please see the screenshot(Gateway), refer the row number #22 in packet capture and please advise what could be the issue. Thanks.

     

    Please capture both messages (from Postman and from DataPower), and save them as raw data.

    Then attach both here (not copy).

    The screenshots do not allow to determined the difference, but there is one obviously.

    Hermann.

  • Sudarshan Bandaru
    Sudarshan Bandaru
    92 Posts

    Re: Multipart/form-data using GatewayScript

    ‏2019-08-22T18:17:29Z  
    • HermannSW
    • ‏2019-08-22T15:25:41Z

    Please capture both messages (from Postman and from DataPower), and save them as raw data.

    Then attach both here (not copy).

    The screenshots do not allow to determined the difference, but there is one obviously.

    Hermann.

    Hi Herman,

     

    I took a packet capture, then follow TCP stream and captured raw messages, please find them.

    It looks to me Postman is sending it as an attachment as I see "Accept-Encoding: gzip, deflate header, please check once.

  • Sudarshan Bandaru
    Sudarshan Bandaru
    92 Posts

    Re: Multipart/form-data using GatewayScript

    ‏2019-08-22T18:34:29Z  

    I am also attaching the packet captures for reference.

    In the Postman packet capture, please refer to row number #18
    In the DataPower packet capture, please refer to row number #4

  • Sudarshan Bandaru
    Sudarshan Bandaru
    92 Posts

    Re: Multipart/form-data using GatewayScript

    ‏2019-08-23T07:09:13Z  

    Hi Herman,

    Looks like I have figured out the problem, now I am able to get success response from backend. I have asked backend team to validate once.

     

    The boundary that appears in the header is two hyphens shorter that the actual boundary in the body.

    Header:

    multipart/form-data; boundary=----WebKitFormBoundarywAlJyAsIQh1QQvZA

     

    Body:

     ------WebKitFormBoundarywAlJyAsIQh1QQvZA

     

    It was really difficult to find out, but the below link helped.

    https://stackoverflow.com/questions/4238809/example-of-multipart-form-data