API Connect

 View Only
  • 1.  Send multipart/form-data As image File to Backend

    Posted Sat May 27, 2023 05:57 AM
      |   view attached
    Hi Team,
     
    I have an requirement to send the base64 encoded image content to backend as form-Data content.
    I have GS ,  which converting the data and sending to backend  but we are getting error from backend as inavalid image also 
    as i have observe that in content ,  having  some junk data in GS output .
    I have GS file  attached ,Can you please help me on this.


    ------------------------------
    mayur gharat
    ------------------------------

    Attachment(s)

    js
    imagejs.js   1 KB 1 version


  • 2.  RE: Send multipart/form-data As image File to Backend

    Posted Mon May 29, 2023 03:46 PM

    HI , can anyone help me on this .
    Also i have attached a part of data which is working with pass through service and  which is not working with GS .
    Non -Working.

    Working 



    ------------------------------
    mayur gharat
    ------------------------------



  • 3.  RE: Send multipart/form-data As image File to Backend

    Posted Thu June 01, 2023 11:06 AM

    Hi Mayur,
    A couple things:  
    First, your code is doing 

    base64Image.toString()

    so there will be utf-8 encoding done by the toString that is changing your binary data.  Just the act of concatenation is probably doing a toString function under the covers and doing the same encoding.  When dealing with a multipart form where you have binary data it is best to build it by creating an array of buffers with the different parts of the overall form, and you can use string concatenation for the ascii portions of that as you're doing, but have your image as a buffer that you simply push to the array, and once you have the entire form in the array, then concatenate the buffers into a new buffer, for example
    var newBuffer = Buffer.concat(newBufferArray);

    Then you would output your newBuffer with the session.output.write function.

    Second, what gateway type are you using?  Your code is doing a 

    session.input.readAsBuffer(function (error, buffer) {

    but other than checking the variable error you never use the variable buffer, so not sure what you're trying to do with this.  What data are you attempting to readAsBuffer?  If you're using a v5 compatible gateway, the input context is the particular input context to the GatewayScript DataPower action within the v5c framework which may not be what you think it is and will most probably give you an empty buffer, and I'm not sure what it would give you if you have an api gateway.


    Best Regards,
    Steve



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



  • 4.  RE: Send multipart/form-data As image File to Backend

    Posted Thu June 01, 2023 06:43 PM
      |   view attached

    Hi Steve Linn ,

    Thanks for  your reply .

    I have done some changes at code level as suggested now the data  seems ok but still from 

    backend  its failing .

     Can you check the attached code is anything missing ?

    Regards ,

    Mayur



    ------------------------------
    mayur gharat
    ------------------------------

    Attachment(s)

    txt
    sample.js.txt   1016 B 1 version


  • 5.  RE: Send multipart/form-data As image File to Backend

    Posted Fri June 02, 2023 08:49 AM

    Hi Mayur,
    Do you have any indication from the backend what it does not like about your request?  Also, since this is the API Connect forum, are you using a v5 compatible gateway or an API Gateway? Or since your code is not using any apim or context functions as I would expect to see in an API GatewayScript policy, but instead is using the header-metadata module to directly set the content-type header,  is this GatewayScript being executed within an action of a Multi-Protocol Gateway Processing policy rule to simply test out the GatewayScript?  Structurally the multipart form looks ok with the boundary of the content type header matching.  The only thing I can think of is the image itself and the base64 decoding in the new Buffer statement.  When you say the data "seems ok" how are you looking at it?  Perhaps you could use as a test the fs module to write that Buffer to a temporary file and view the contents?

    Best Regards,
    Steve



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



  • 6.  RE: Send multipart/form-data As image File to Backend

    Posted Sat June 03, 2023 09:54 AM
    Edited by mayur gharat Sun June 11, 2023 05:35 PM

    Hello Steve ,

    We are using v5 compatible gateway .

    I have tried with other code also with apim function but still error is same .(500 

    errorMessage": "internal error",

    )

    Data looks ok with this script also .

    Data from GS which is not working 

     

    Data from Pass through which is working

     

    sample2.js

    var hm = require('header-metadata');
    apim.readInputAsBuffer(function (error, buffer) {
     
      if (error)
     
      {
     
        // handle error
     
        session.output.write (error.errorMessage);
     
       } else {
     
     
     
     
     
    var base64Image = "base64 encoded data "
     
     
     
    var decoded = new Buffer(base64Image,'base64')
     
     
    var prefix = `----------------------------921934391642645198236509
    Content-Disposition: form-data; name="wsq_file"; filename="test.wsq"
    Content-Type: application/octet-stream
     
    `;
    var suffix = `
    ----------------------------921934391642645198236509--
    `;
     
    var buf1 = Buffer.from(prefix);
    var buf2 = Buffer.from(decoded);
    var buf3 = Buffer.from(suffix);
            var arr1 = [buf1, buf2, buf3];
            var newBuff = Buffer.concat(arr1);
    var contentType = 'multipart/form-data; boundary=' + '--------------------------921934391642645198236509';
            apim.setvariable('message.headers.Content-Type', contentType);
           // hm.current.set('Content-Type', contentType);
            session.output.write(newBuff);
             console.error(newBuff);
         apim.output(contentType);
     
       }
     
    });

    Regards ,

    Mayur



    ------------------------------
    mayur gharat
    ------------------------------



  • 7.  RE: Send multipart/form-data As image File to Backend

    Posted Mon June 05, 2023 03:33 PM

    Hi Mayur,
    Nothing jumps out at me with your code as being an issue.  A 500 internal error has to have written something in the DataPower default logs to give you a clue as to what is failing.  If you can show me the result of your GatewayScript as you have above it isn't that but must be something else in your assembly or the v5c framework.  DataPower logs are your friend :-) I'd enable debug logging in your APIC v5c domain.  You can also enable a DataPower probe in v5c and at least see where the webapi MPGW is failing.

    Best Regards,
    Steve



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