3
votes

I want to send files from Node JS through http module using multipart/form-data content-type. The problem with binary encoding. It's OK when I'm trying to send simple text file:

url: 'some-site.com',
method: 'POST',
headers: 
{
    'content-type': 'multipart/form-data; boundary=-----BNDRY',
    'content-length': 128
},
body: '-------BNDRY\r\ncontent-type: text/plain\r\ncontent-disposition: form-data; name="file"; filename="file.txt"\r\n\r\ntest\r\n-------BNDRY--'
}

But when I'm trying to send something like JPG after file reading (e.g. via FS modile) and translate Buffer to string for request body it fails. I've tried different combinations of Buffer.toString(encoding) method and content-transfer-encoding: encoding header but there was no success. For some reason, base64 encoding doesn't work too, I've tested it with connect bodyParser, and seems like it doesn't care about content-transfer-encoding: base64 header in body - content still comes as undecoded base64 string.

And I don't want to use external modules like node-formidable or express to solve my problem.

Thanks.

1

1 Answers

-3
votes

I solved it by using Buffer instead of String