nodejs multipart/formData Request - node.js

I am noobie in multipart/formdata request in node js.
I am crawling a website which does multipart/formdata request.This is how it does :
url : https://example.com
Content-Type: multipart/form-data;boundary=----
formdata :
------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="to"
destination#email.com
------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="from"
recipient#email.com
------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="subject"
subject line
------------------------------7a9cd2dc11c1
Content-Disposition: form-data; name="text"
This content does not matter.
------------------------------7a9cd2dc11c1--
am using request module of node js for doing that. Help, i need example..

Related

How can I use Axios to send a request to this API when the example is given using raw text format?

I am trying to use concord technologies cloud fax tool. The goal is to set up incoming and outgoing fax service from a nodejs express server.
This is an example of the 'raw text format' from concord's website. I am not sure how I can convert this to a cal using axios.
POST /api/intake/fax-notify HTTP/1.1
Host: https://nextstep.concord.net
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="FaxMessageId"
bbe58db7-b3f3-461a-9c47-5b803586cc53
Content-Disposition: form-data; name="MessageType"
Inbound
Content-Disposition: form-data; name="AttachmentName"
bbe58db7-b3f3-461a-9c47-5b803586cc53.tif
Content-Disposition: form-data; name="FaxSenderCSID"
Test User (123-555-1212)
Content-Disposition: form-data; name="CallerNumber"
1-123-555-1212
Content-Disposition: form-data; name="CalledNumber"
1-123-555-6789
Content-Disposition: form-data; name="FaxReceivedDateTime"
10/07/2018 10:45:21 AM
Content-Disposition: form-data; name="FaxRecipientTimeZone"
(GMT-06:00) Central Time (US & Canada)
Content-Disposition: form-data; name="FaxPages"
1
Content-Disposition: form-data; name="FaxResolution"
1
Content-Disposition: form-data; name="FaxSpeed"
2
Content-Disposition: form-data; name="AttachmentCount"
1
Content-Disposition: form-data; name="Account"
12345
Content-Disposition: form-data; name="AuthUser"
12345-6789-1212-12345-0001
Content-Disposition: form-data; name="File"; filename="bbe58db7-b3f3-461a-9c47-5b803586cc53.tif"
Content-Type: file
<binary file data>
------WebKitFormBoundary7MA4YWxkTrZu0gW--
Any help would be greatly appreciated, ty.

InvalidParameterValue: Duplicate header 'Content-Transfer-Encoding'

when I am trying to send an attachment with an email I got this error can anyone tell me what am I doing wrong here is the code which i have tried
var payload = `From: 'Amarjeet Singh' <${sender}>
To: ${recipient}
Subject: AWS SES Attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=7f1a313ee430f85a8b054f085ae67abd6ee9c52aa8d056e7f7e19c6e2887
--NextPart
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
This is the <b>body</b> of the email.
--7f1a313ee430f85a8b054f085ae67abd6ee9c52aa8d056e7f7e19c6e2887
Content-Type: application/json; charset=UTF-8;
Content-Disposition: attachment; filename="colors.json"
Content-Transfer-Encoding: base64
${file}
--NextPart--`;
var params = {
RawMessage: {
Data: payload
},
Source: "xyz#gmail.com"
};
If you are using AWS SES your payload has two issues, first your boundary is different, and second just keep in mind the line breaks, your string template should be like this:
var payload = `From: 'Amarjeet Singh' <${sender}>
To: ${recipient}
Subject: AWS SES Attachment
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary=7f1a313ee430f85a8b054f085ae67abd6ee9c52aa8d056e7f7e19c6e2887
--7f1a313ee430f85a8b054f085ae67abd6ee9c52aa8d056e7f7e19c6e2887
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
This is the <b>body</b> of the email.
--7f1a313ee430f85a8b054f085ae67abd6ee9c52aa8d056e7f7e19c6e2887
Content-Type: application/json; charset=UTF-8;
Content-Disposition: attachment; filename="colors.json"
Content-Transfer-Encoding: base64
${file}
--7f1a313ee430f85a8b054f085ae67abd6ee9c52aa8d056e7f7e19c6e2887--`;

How can be parse `multipart/mixed` in node.js?

I see lot of module in node.js like busboy , multer , formidable but they are not able to parse multipart/mixed body.
Example of the request multipart/mixed in node.js
--batchbatch
Content-Type: multipart/mixed; boundary=changeset1
--changeset1
Content-Type: application/http
Content-Transfer-Encoding: binary
POST data HTTP/1.1
Content-Type: application/json
Content-ID: ID1
Content-Length: 10000
{
"Name": "ddd",
"data1": "3",
"data2": "40",
"data3": "OPPT",
"data3":"D1000",
"dat4": "D1000"
}
--changeset1--
--batchbatch
Content-Type: multipart/mixed; boundary=changeset2
--changeset2
Content-Type: application/http
Content-Transfer-Encoding: binary
PATCH data/32451 HTTP/1.1
Content-Type: application/json
Content-ID: ID1
Content-Length: 10000
{
"code": "3"
}
--changeset2--
--batchbatch
Content-Type: multipart/mixed; boundary=changeset3
--changeset3
Content-Type: application/http
Content-Transfer-Encoding: binary
DELETE /data/2345 HTTP/1.1
--changeset3--
--batchbatch--

INVALID_REQUEST_PARAMETER - filename not found in Content-Disposition header of multipart form

I am attempting to add a document by multipart form to an existing draft envelope via the Docusign REST API endpoint: /envelopes/[envelopeId]/documents/[documentId]
The error message I get is:
The request contained at least one invalid parameter. A filename was not
found in the Content-Disposition header ('filename="filename.ext"
As you can see, there is a filename parameter in the Content-Disposition value. I've tried multiple different edits and adjustments of the Content-Disposition header, but each has failed. Here is what appears to be the correct format that throws the error.
Headers:
Authorization "Bearer [token]"
Accept "application/json"
Content-Type "multipart/form-data; boundary=AAAAAA"
--AAAAAA
Content-Disposition: form-data
Content-Type: application/json
{"fileExtension":"pdf","name":"test file name.pdf","documentId":1,"order":1}
--AAAAAA
Content-Disposition: file; filename="test file name.pdf"; documentId=1
Content-Type: application/pdf
[binary output]
--AAAAAA--
Anyone from Docusign or in general able to see what's wrong or give me a hand? Thanks.
multipart/form-data will not work for the updateEnvelopeDocument api
Instead specify the Content-Type and content-disposition in the Headers. Request body should only contain the file stream.
put /envelopes/[envelopeId]/documents/[documentId]
[Headers]
Content-Type: application/pdf
content-disposition: file; filename="test file name.pdf"; fileExtension=pdf; documentId=1
[Body]
[binary output]
The other option is to use the updateListEnvelopeDocuments api which supports multipart/form-data. You can also choose to update multiple documents in a single api call using this api.
PUT /v2/accounts/{accountId}/envelopes/{envelopeId}/documents
Headers:
Authorization "Bearer [token]"
Accept "application/json"
Content-Type "multipart/form-data; boundary=AAAAAA"
--AAAAAA
Content-Type: application/json
Content-Disposition: form-data
{
"documents": [
{
"documentId": 1,
"fileExtension": "pdf",
"name": "test file name.pdf"
}
]
}
--AAAAAA
Content-Type: application/pdf
Content-Disposition: file; filename="test file name.pdf"; fileExtension=pdf; documentId=1
Content-Transfer-Encoding: stream
[Binary output]
--AAAAAA--

Python 3, using requests with POST but 302 status and strange request payload headers

Trying to use the Python Requests module to post to a form:
using Chrome/inspect/network tab after I search for something is
shows:
Request
URL:https://www.fbo.gov/index?s=opportunity&mode=list&tab=search
Status Code:302 Found
The Request Payload is:
... begin snippet ....
procurement_notice
------WebKitFormBoundary9stOUHDiQLr2yrEB Content-Disposition: form-data; name="dnf_class_values[procurement_notice][notice_id]"
8045295a672345856701e8ff0ab87a4c
------WebKitFormBoundary9stOUHDiQLr2yrEB Content-Disposition: form-data;
name="dnf_class_values[procurement_notice][_so_agent_save_agent]"
------WebKitFormBoundary9stOUHDiQLr2yrEB Content-Disposition: form-data;
name="dnf_class_values[procurement_notice][custom_response_date]"
------WebKitFormBoundary9stOUHDiQLr2yrEB Content-Disposition: form-data;
name="dnf_class_values[procurement_notice][custom_posted_date]"
30
------WebKitFormBoundary9stOUHDiQLr2yrEB Content-Disposition: form-data; name="dnf_class_values[procurement_notice][keywords]"
Colorado
... end snippet ....
The code, I am trying is:
import requests
headers ={'X-Requested-With': 'XMLHttpRequest',
'User-Agent': 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8 GTB7.1 (.NET CLR 3.5.30729)'}
url = 'https://www.fbo.gov/index?s=opportunity&mode=list&tab=search'
search_data={"dnf_class_values[procurement_notice][keywords]":Colorado}
r = requests.post(url, data=search_data, headers=headers)
it doesn't give me the r.content as if I passed in the search term 'Colorado'.
So my question is does the 'Code:302', which is a re-direct, mean they don't accept a POST request and also how do I deal with the 'WebKitFormBoundary' ...'Content-Disposition: form-data...' as it doesn't look like a normal dictionary element I can pass in.

Resources