node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7.01k stars 680 forks source link

Is there any way to modify multipart form-data field #753

Closed AmolM27 closed 2 years ago

AmolM27 commented 2 years ago

I am currently trying to implement a proxy that modifies field 'filename' and relay that in the proxy request. Formidable is perfect to retreive the value of the field via: form.on('field', function(field, value) { //receive form fields here console.log(field.filename) }) However, can it be updated to take another another value rather than just reading it?

GrosSacASac commented 2 years ago

Question unclear.

The fields object can be changed after the .parse callback.

How are you relaying the request ?

AmolM27 commented 2 years ago

So here is code example. I basically want to change the field value to be 'test1.docx' for example. The 'filename' field is set to: 81_105_115_116_54_46_100_111_99_120. I will need to change that from ascii to string before relaying the request. This field is part of the multipart form data. To put some context I mean that field 'name="filename"\r\n' + '\r\n' + to change if that is making sense?

var app = require('express')(); const { createProxyMiddleware } = require('http-proxy-middleware'); var formidable = require('formidable')

const filter = function (pathname, req) {

var form = new formidable.IncomingForm();
form.on('field', function(field, value) {
    //receive form fields here
    value = 'test1.docx'
    return value;
})
return true;

};

app.use('*', createProxyMiddleware(filter, { target: "", changeOrigin: true, })).listen(10008);

GrosSacASac commented 2 years ago

I don't know about createProxyMiddleware, try again on stack overflow maybe