Digital Assistant and Mobile

Get Involved. Join the Conversation.

Topic

    Jijo Joshi
    How to write a custom API that accepts Multipart form data...
    Topic posted August 3, 2017 by Jijo JoshiRed Ribbon: 250+ Points, last edited February 5, 2018 
    148 Views, 10 Comments
    Title:
    How to write a custom API that accepts Multipart form data & converts that to base 64 in custom code?
    Summary:
    How to write a custom API that accepts Multipart form data & converts that to base 64 in custom code?
    Content:

    Hi,

    I want to write a custom API that accepts multipart form data & some additional parameters as query parameter/ request body input. We have a connector API that accepts base64 for image upload.
    So we want to accept multipart form data in our custom API, convert it to base64 internally in our code & call that connector API. Is there any example for custom code that accepts multipart form data in Custom APIs? How can we achieve this? we also want to accept some additional inputs in our API via uery/body parameters. Could someone please help us?

    Comment

     

    • Tom Ware

      Custom Code uses the Javascript Express module, so you handle multipart messages using express's built in support.

      Depending which version of NodeJS you are using in Custom code, the strategy will be somewhat different.

      Using our support for node 6.10, you will need the multer middleware since it uses express 4:

      https://expressjs.com/en/resources/middleware/multer.html

      I haven't tried it yet, but it should be a matter of creating the right variables and using them.  If you have problems, let me know what they are and I'll help out.

      var multer  = require('multer')
      var upload = multer({ dest: 'uploads/' })

      The doc above gives some example code.

      If you are using our older node 0.10 support, it uses express 3 instead.  I recommend using 6.10, so let us know if you are using the older version and I'll track down an example.

      As for base64 conversion, there are a number of node modules for base64 conversion.  Here is one: https://www.npmjs.com/package/base-64

    • Jijo Joshi

      Hi,
      we are using node version 0.10. (CCC_DefaultNodeConfiguration=0.10) Could you please help us by providing an example that accepts a file as multipart form-data in our custom code?. I got some examples converting files to base 64 from MCS Documentation. But couldn't find any examples related to multipart form-data.

      if (req.is('json') || req.is('application/x-www-form-urlencoded'))

        {

        console.info('Request Body: ' + JSON.stringify(req.body));

        } else {

        var data = [];

        // Process a chunk of data. This may be called multiple times.

        req.on('data', function(chunk) {

        // Append to buffer

        data.push(chunk);

        }).on('end', function() {

        // process full message here

        var buffer = Buffer.concat(data);

        // Convert to base64, if required

        // var base64 = buffer.toString('base64');

        });

        }

    • Jijo Joshi

      One more question, if i change the node version to 6.10 in policy files, will it affect my other APIs that is written in 0.10 before. I mean the other APIs which i have already developed?
      we have used several other npm libraries too in our code.

    • Tom Ware

      I do have an example of multipart lying around somewhere for node 0.10.  I however, recommend moving to 6.10.  You can do that for a single API:

      Implementing Custom APIs

      The package.json fragment looks like this:

      "oracleMobile" : {

        "configuration" : {

        "node" : "6.10" }

        }

      If you change the policy instead, it will change all your apis.

    • Tom Ware

      I will have to dig around a bit for the multipart example in express 3.  I will we away from my computer until tuesday.  'will look then.

    • Jijo Joshi

      Thanks Tware. I changed the configuration of that single API to 6.10 & used multer as suggested. It worked. Thanks a lot.
      Is 6.10 backward compatible ? If i change the node configuration in my policy file, will it be compatible with the APIs i have already developed?

    • Tom Ware

      For the most part, the functionality we use in 6.10 is compatible with the same functionality in 0.10.  There are, however, some differences in the dependencies.  For instance, if you are using anything more than the basic functionality in Express, there are some differences.  (Express is the library we use to expose REST functionality, so it would be affected by operations related to things in your js code like:

      operations on "service" here:

      module.exports = function (service) {

      operations on any of the variables in lines like this:

      service.get('/mobile/custom/Foo/Bar', function (req, res) {

      If you are just using the basic functionality, everything will likely work, but if you have other APIs that use extended functionality from Express, there may be some issues.

      All of the above is really just so say, I think most apps will work, but I would recommend testing.

    • Jijo Joshi

      Ok. Thanks Tware

    • Devadeekshith Reddy

      Hi Jijo

      We are having a same requirement for uploading a mulitpart form data.

      Can you please share me some insight on how the Custom API code looks like.

      Thanks in advance

      Thanks

      Deekshith

    • Bhargavi Chodisetti

      Hi,

      Can you please help to create custom api to upload file to webservice.

      Thanks