Handle multipart/form-data with Serverless? -
how handle multipart/form-data serverless-framework? v.0.5.6
just tried this:
"requesttemplates": { "multipart/form-data": { "httpmethod": "$context.httpmethod", "body": "$input.json('$')", "queryparams": "$input.params().querystring", "headerparams": "$input.params().header", "headerparamnames": "$input.params().header.keyset()", "contenttypevalue": "$input.params().header.get('content-type')" }, "application/json": { "httpmethod": "$context.httpmethod", "body": "$input.json('$')", "queryparams": "$input.params().querystring", "headerparams": "$input.params().header", "headerparamnames": "$input.params().header.keyset()", "contenttypevalue": "$input.params().header.get('content-type')" } }
action.js:
export function respond(event, cb) { var form = new formidable.incomingform(); form.parse(event, function(err, fields, files) { if (err == null) { var response = { status: "true", data: fields, error: [] }; return cb(null, response); } else { console.log(err); return cb(null, apierrors.errors(402, err['message'] + fields)); } }); }
but got error: errormessage = "cannot read property 'content-length' of undefined";
i've got working serverless emulating http.clientrequest , using form parser tool formidable.
i'm using lambda-proxy
api gateway event configuration.
const stream = require('stream').readable; const formidable = require('formidable'); module.exports.upload = ( e, ctx, cb ) => { let form = new formidable.incomingform(); let stream = new stream(); stream.push( e.body ); stream.push( null ); // note: you'll want tolowercase() @ least 'content-type' header key stream.headers = e.headers; form.parse( stream, (err, fields, files) => { // work parsed form results here. }); }
Comments
Post a Comment