Sample API that support Logic App chunking using Node js
Published Jul 18 2021 10:32 PM 3,050 Views

This article to shows how build simple Node js endpoint that can react to the logic app chunking http action  

The API was implemented by following the article  Handle large messages by using chunking - Azure Logic Apps | Microsoft Docs

The logic app uses chucking when sending data to managed API like SFTP for example.

 

The HTTP action is not relying on the request header “Transfer-Encoding”. It implements its own chunking protocol by using custom Header key “x-ms-transfer-mode” instead. 

 

The below sequence diagram shows the minimum implementation for the Server component since some of the headers are optional.

 

Sequence diagram for chunking flow

 

Mohammed_Barqawi_2-1626639118079.png

 

 

Source code for the server .js

 

 

'use strict';

var express = require('express');
var app = express();

app.set("port", process.env.PORT || 4000);

app.get('/', function (req, res) {
   res.writeHead(200, { 'Content-Type': 'application/json' });
   var response = { "response": "This is GET method." }
   console.log(response);
   res.end(JSON.stringify(response));
})

app.get('/:id', function (req, res) {
   res.writeHead(200, { 'Content-Type': 'application/json' });
   var response = { "response": "This is GET method with id=" + req.params.id + "." }
   console.log(response);
   res.end(JSON.stringify(response));
})

app.post('/', function (req, res) {
   res.writeHead(200, { 'Content-Type': 'application/json' });
   var response = { "response": "This is POST method." }
   console.log(response);
   res.end(JSON.stringify(response));
})
app.patch('/*', function (req, res) {
   console.log('Start patch method');
   var Content_Range = req.headers['content-range']
   console.log("All headers Patch" + JSON.stringify(req.headers));
   console.log("Content_Range" + Content_Range);
 
   var Byte_Rang=Content_Range.match(/(\d\-\d*)/gm)
   console.log("Byte_Rang" + Byte_Rang);

   res.writeHead(200, { 'Content-Type': 'application/json','Range': "bytes="+Byte_Rang });
   var response = { "response": "This is patch method." }
   console.log(response);
   res.end(JSON.stringify(response));
})

app.put('/', function (req, res) {
   const url = getFullUrl(req)
   console.log('Start PUT method.');
   console.log("All headers for PUT" + JSON.stringify(req.headers));


   var transfer_mode = req.headers['x-ms-transfer-mode']
   console.log(transfer_mode);
   res.set({
      'Content-Type': 'text/plain',
      'Location': url + '/patch'
   })
   res.writeHead(200);
   var response = { "response": "This is PUT method." }
   console.log(response);
   res.end(JSON.stringify(response));
})



var server = app.listen(app.get("port"), function () {

   var host = server.address().address
   var port = server.address().port

   console.log("Node.js API app listening at http://%s:%s", host, port)

})



/**
 * Gets the self full URL from the request
 * 
 * @param {object} req Request
 * @returns {string} URL
 */
const getFullUrl = (req) => `${req.protocol}://${req.headers.host}${req.originalUrl}`;

 

 

the Request Header for the PUT request 

 

{
    "host": "chnuck2fromcode.azurewebsites.net",
    "x-client-ip": "13.95.147.65",
    "x-client-port": "36368",
    "connection": "Keep-Alive",
    "content-length": "0",
    "accept-encoding": "gzip, deflate",
    "accept-language": "en",
    "max-forwards": "10",
    "user-agent": "azure-logic-apps/1.0 (workflow 5c6576d670914f0c8345b4bcf3435f3c; version 08585750132920657062)",
    "x-ms-transfer-mode": "chunked",
    "x-ms-content-length": "44",
    "x-ms-workflow-id": "5c6576d670914f0c8345b4bcf3435f3c",
    "x-ms-workflow-version": "08585750132920657062",
    "x-ms-workflow-name": "httptest",
    "x-ms-workflow-system-id": "/locations/westeurope/scaleunits/prod-215/workflows/5c6576d670914f0c8345b4bcf3435f3c",
    "x-ms-workflow-run-id": "08585750114452676750935970680CU211",
    "x-ms-workflow-run-tracking-id": "c5f53ac5-8d65-4200-89f4-65361258ee64",
    "x-ms-workflow-operation-name": "HTTP",
    "x-ms-execution-location": "westeurope",
    "x-ms-workflow-subscription-id": "----------",
    "x-ms-workflow-resourcegroup-name": "CSS",
    "x-ms-tracking-id": "1adc815f-a758-4191-9230-b4761b7b65f7",
    "x-ms-correlation-id": "1adc815f-a758-4191-9230-b4761b7b65f7",
    "x-ms-client-request-id": "1adc815f-a758-4191-9230-b4761b7b65f7",
    "x-ms-client-tracking-id": "08585750114452676750935970680CU211",
    "x-ms-action-tracking-id": "b7a86846-f6d8-44e0-a8dd-ab089e8ce907",
    "x-ms-activity-vector": "IN.0J.IN.06",
    "x-waws-unencoded-url": "/",
    "client-ip": "13.95.147.65:36368",
    "x-arr-log-id": "bec9d203-20ab-40b2-9463-e433bbb4b765",
    "disguised-host": "chnuck2fromcode.azurewebsites.net",
    "x-site-deployment-id": "chnuck2fromcode",
    "was-default-hostname": "chnuck2fromcode.azurewebsites.net",
    "x-original-url": "/",
    "x-forwarded-for": "13.95.147.65:36368",
    "x-arr-ssl": "2048|256|C=US, O=Microsoft Corporation, CN=Microsoft RSA TLS CA 01|CN=*.azurewebsites.net",
    "x-forwarded-proto": "https",
    "x-appservice-proto": "https",
    "x-forwarded-tlsversion": "1.2"
}

 

 

The Request header for the Patch request 

 

{
    "host": "chnuck2fromcode.azurewebsites.net",
    "x-client-ip": "13.95.147.65",
    "x-client-port": "18752",
    "connection": "Keep-Alive",
    "content-length": "44",
    "content-type": "text/plain; charset=utf-8",
    "content-range": "bytes 0-43/44",
    "accept-encoding": "gzip, deflate",
    "accept-language": "en",
    "max-forwards": "10",
    "user-agent": "azure-logic-apps/1.0 (workflow 5c6576d670914f0c8345b4bcf3435f3c; version 08585750132920657062)",
    "x-ms-workflow-id": "5c6576d670914f0c8345b4bcf3435f3c",
    "x-ms-workflow-version": "08585750132920657062",
    "x-ms-workflow-name": "httptest",
    "x-ms-workflow-system-id": "/locations/westeurope/scaleunits/prod-215/workflows/5c6576d670914f0c8345b4bcf3435f3c",
    "x-ms-workflow-run-id": "08585750114452676750935970680CU211",
    "x-ms-workflow-run-tracking-id": "c5f53ac5-8d65-4200-89f4-65361258ee64",
    "x-ms-workflow-operation-name": "HTTP",
    "x-ms-execution-location": "westeurope",
    "x-ms-workflow-subscription-id": "31511bf2-385b-420e-b190-b14268a94dcf",
    "x-ms-workflow-resourcegroup-name": "CSS",
    "x-ms-tracking-id": "448096a7-0955-4841-954b-0638015581b5",
    "x-ms-correlation-id": "448096a7-0955-4841-954b-0638015581b5",
    "x-ms-client-request-id": "448096a7-0955-4841-954b-0638015581b5",
    "x-ms-client-tracking-id": "08585750114452676750935970680CU211",
    "x-ms-action-tracking-id": "b7a86846-f6d8-44e0-a8dd-ab089e8ce907",
    "x-ms-activity-vector": "IN.0J.IN.08",
    "x-waws-unencoded-url": "//patch",
    "client-ip": "13.95.147.65:18752",
    "x-arr-log-id": "800de470-2d12-423b-afdd-c1f4f4bf8f92",
    "disguised-host": "chnuck2fromcode.azurewebsites.net",
    "x-site-deployment-id": "chnuck2fromcode",
    "was-default-hostname": "chnuck2fromcode.azurewebsites.net",
    "x-original-url": "/patch",
    "x-forwarded-for": "13.95.147.65:18752"
}

 

 

Co-Authors
Version history
Last update:
‎Jul 18 2021 10:32 PM
Updated by: