6

Is there a Node.js library for generating multipart/form-data content in the following form?

------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="to"

[email protected]
------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="from"

[email protected]
------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="subject"

subject line
------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="text"

This content does not matter.
------------------------------7a9cd2dc11c1--
2

3 Answers 3

4

fermata looks like it might be what you're looking for. From the github page:

fermata.json("http://example.com/some/action").post({
  'Content-Type':"multipart/form-data"
}, {
  fileField: form.input.file || {data:nodeBuffer, name:"", type:""}
}, callback)
Sign up to request clarification or add additional context in comments.

1 Comment

If we want to send the same request. How we will send ? I have tried with request module. stackoverflow.com/questions/51274505/… Please help
1

request supports multipart/form-data or if you're just looking to create the content body, try form-data

1 Comment

If we want to send the same request. How we will send ? I have tried with request module. stackoverflow.com/questions/51274505/… Please help
1

I know this is old but I recently needed to generate a form's multipart/form-data body string and came across this thread.

I was unable to find any modules out there that did this in a simple way so I made my own module: https://github.com/kodie/form-data-body

You can use it like so:

const formDataBody = require('form-data-body')

// Specify form fields
const fields = {
  name: 'My test post',
  description: 'This is just a test post',
  items: ['First Item', 'Second Item'],

  // Files should be an object with the name, type, and data set to strings
  image: {
    name: 'hello.jpg',
    type: 'image/jpeg',
    data: binaryImageData
  }
}

const boundary = formDataBody.generateBoundary()
const header = {
  'Content-Type': `multipart/form-data; boundary=${boundary}`
}
const body = formDataBody(fields, boundary)

Example Output:

----------------------------071517909670537006900435
Content-Disposition: form-data; name="name"

My test post
----------------------------071517909670537006900435
Content-Disposition: form-data; name="description"

This is just a test post
----------------------------071517909670537006900435
Content-Disposition: form-data; name="items[]"

First Item
----------------------------071517909670537006900435
Content-Disposition: form-data; name="items[]"

Second Item
----------------------------071517909670537006900435
Content-Disposition: form-data; name="image"; filename="hello.jpg"
Content-Type: image/jpeg

[BINARY IMAGE DATA]
----------------------------071517909670537006900435--

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.