项目作者: Rantelo

项目描述 :
Multipart/form-data parser
高级语言: JavaScript
项目地址: git://github.com/Rantelo/mortyparse.git
创建时间: 2017-11-25T01:12:20Z
项目社区:https://github.com/Rantelo/mortyparse

开源协议:

下载


Mortyparse

Parse a Buffer containing a multipart/data-form

Usage

Grab the boundary from the content-type in the POST request and create and instance of Mortyparse.

Then, with the base64 string from the body-json, use the Mortyparse instance to parse the form-data into a readable format.

  1. const Morty = require('mortyparse');
  2. const b64_str = '<base64 string>'; //containing the multipart/form-data as received in the server
  3. morty = new Morty(Buffer("------WebKitFormBoundaryaVKetDdeBHAjicUm"));
  4. let parsed = morty.parse(Buffer(b64_str, 'base64'));
  5. console.log(parsed); // =>
  6. // [
  7. // {
  8. // "content_type": "application/pdf",
  9. // "data": Buffer <37 80 68 . . . 33 13 10>,
  10. // "filename": "testpdf.pdf",
  11. // "name": "cv"
  12. // },
  13. // { "name": "name", "data": "Test Name"},
  14. // { "name": "email", "data": "test@email.com"},
  15. // { "name": "telephone", "data": "0101010101"},
  16. // { "name": "vacant", "data": "Back-End"}
  17. // { "name": "reference", "data": "internet"},
  18. // { "name": "portfolio", "data": "No aplica"}
  19. // ]

Output format

The parser returns an array containing an object per input in the form.

All inputs in the parsed data will have:

  • name: with a String representing the name of the input field
  • data: with a String|Buffer representing the content of the input
    • Buffer in the case of file inputs
    • String in the case of other inputs

File type inputs will also have:

  • content_type: with a String representing the file format type
  • filename: with a String containing the file name