1

How do I add add an extra parameter to a multipart HTML form using Javascript? For a normal form, I am overriding the form submit method and appending the data like here -

(function (submit) {
            HTMLFormElement.prototype.submit = function () {
                var tokenValue = $('#_csrf');
                //var tokenValue=token.val();
                tokenValue.appendTo(this);      
                submit.call(this);
            };
})(HTMLFormElement.prototype.submit);

And it gets it get attached like this -

_csrf:b0b386ae-7c33-4941-ad23-afd42ee88711

Where as for multipart form, it appears like this -

------WebKitFormBoundary7BYn6AhxBkI3hnrG Content-Disposition: form-data; name="_csrf"

i.e. only the name of the parameter

  • What are you trying to add to `multipart/form-data`? A key, value pair? Or only modify portion of existing data? Can you include `html` at Question? – guest271314 May 26 '17 at 01:17
  • Extending native HTMLElement interfacess with prototype is considered a bad practice. // Don't over-complicate this - add a hidden field to the form, put the CSRF token in there, and let the usual submission mechanism do its work. – CBroe May 26 '17 at 01:18
  • @CBroe, I do not have access to the forms, only writing a single point of exit script that appends token to form – user8055937 May 26 '17 at 01:21
  • You can use JavaScript in the scope of the page, yes? Then you do have access to the forms. – CBroe May 26 '17 at 01:24
  • @CBroe, my script is in a header that gets attached to the page - some pages may have forms some do not. So I am adding token in a hidden field in the header itself then attaching it to form just before request goes out. – user8055937 May 26 '17 at 01:27
  • @user8055937 You can use `FormData.prototype.append()` to append key, value pairs to `FormData` object, or modify raw `multipart/form-data`. See [multipart/form-data form with no field names](https://stackoverflow.com/questions/43590899/multipart-form-data-form-with-no-field-names/), [Get HTTP Body of Form in JavaScript](http://stackoverflow.com/questions/40111982/get-http-body-of-form-in-javascript) – guest271314 May 26 '17 at 02:56

0 Answers0