Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing the data in multipart/form-data in Lambda(Node.js)

I want parsing the data when I send the data by multipart/form-data, How I can parsing the response data?

I use the API Gateway + Lambda,

So I set the body mapping templates,

Content-Type : multipart/form-data

{
  "body" : $input.json('$'),
  "headers": {
    #foreach($header in $input.params().header.keySet())
    "$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end

    #end
  },
  "method": "$context.httpMethod",
  "params": {
    #foreach($param in $input.params().path.keySet())
    "$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end

    #end
  },
  "query": {
    #foreach($queryParam in $input.params().querystring.keySet())
    "$queryParam": "$util.escapeJavaScript($input.params().querystring.get($queryParam))" #if($foreach.hasNext),#end

    #end
  }  
}

And In lambda, just confirm the data sending

const AWS = require('aws-sdk');
const querystring = require('querystring');

exports.handler = (event, context, callback) => {


    callback(null,event.body);


};

So response result is like this,

"----------------------------249221450291104233730977\r\nContent-Disposition: form-data; name=\"data\"\r\n\r\n1\r\n----------------------------249221450291104233730977\r\nContent-Disposition: form-data; name=\"file\"; filename=\"SampleJPGImage_15mbmb.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n\ufffd\ufffd\ufffd\ufffd\u0000\u0010JFIF\u0000\u0001\u0001\u0001\u0000H\u0000H\u0000\u0000\ufffd\ufffd\u0000\"Exif\u0000\u0000MM\u0000*\u0000\u0000\u0000\b\u0000\u0001\u0001\u0012\u0000\u0003\u0000\u0000\u0000\u0001\u0000\u0001\u0000\u0000\u0000\u0000\u0000\u0000\ufffd\ufffd\fXICC_PROFILE\u0000\u0001\u0001\u0000\u0000\fHLino\u0002\u0010\u0000\u0000mntrRGB XYZ \u0007\ufffd\u0000\u0002\u0000\t\u0000\u0006\u00001\u0000\u0000acspMSFT\u0000\u0000\u0000\u0000IEC sRGB\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\ufffd\ufffd\u0000\u0001\u0000\u0000\u0000\u0000\ufffd-HP  \u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0011cprt\u0000\u0000\u0001P\u0000\u0000\u00003desc\u0000\u0000\u0001\ufffd\u0000\u0000\u0000lwtpt\u0000\u0000\u0001\ufffd\u0000\u0000\u0000\u0014bkpt\u0000\u0000\u0002\u0004\u0000\u0000\u0000\u0014rXYZ\u0000\u0000\u0002\u0018\u0000\u0000\u0000\u0014gXYZ\u0000\u0000\u0002,\u0000\u0000\u0000\u0014bXYZ\u0000\u0000\u0002@\u0000\u0000\u0000\u0014dmnd\u0000\u0000\u0002T\u0000\u0000\u0000pdmdd\u0000\u0000\u0002\ufffd\u0000\u0000\u0000\ufffdvued\u0000\u0000\u0003L\u0000\u0000\u0000\ufffdview\u0000\u0000\u0003\ufffd\u0000\u0000\u0000$lumi\u0000\u0000\u0003\ufffd\u0000\u0000\u0000\u0014meas\u0000\u0000\u0004\f\u0000\u0000\u0000$tech\u0000\u0000\u00040\u0000\u0000\u0000\frTRC\u0000\u0000\u0004<\u0000\u0000\b\fgTRC\u0000\u0000\u0004<\u0000\u0000\b\fbTRC\u0000\u0000\u0004<\u0000\u0000\b\ftext\u0000\u0000\u0000\u0000Copyright (c) 1998 Hewlett-Packard Company\u0000\u0000desc\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0012sRGB IEC61966-2.1\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0012sRGB IEC61966-2.1\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000\u0000XYZ.."

In postman I send the data like this, enter image description here

So How I can take the data by parameter?

like image 532
Polaris Nation Avatar asked Jun 28 '18 05:06

Polaris Nation


1 Answers

Are to trying to do file uploads via Lambda? It might be easier, more cost effective and more robust to handle this e.g. via a S3 upload with temporary credentials.

To answer your direct question; 'multipart/form-data' is not JSON. So you need to pass it on raw:

{ "body" : $input.body,

Now in NodeJS you can handle event.body with some library that handles multipart/form-data. You can read more about interepreting this format in this question.

like image 82
Bram Avatar answered Sep 28 '22 00:09

Bram