Fix browsable API not supporting multipart/form-data correctly

- Autodetect missing boundary parameter for Content-Type header
- textarea value normalises EOL chars to \n when multipart/form-data requires \r\n
This commit is contained in:
Levi Cameron 2017-05-25 20:07:34 +10:00
parent 871ce34983
commit 94c37c09c5

View File

@ -37,6 +37,17 @@ function doAjaxSubmit(e) {
if (contentType) {
data = form.find('[data-override="content"]').val() || ''
if (contentType === 'multipart/form-data') {
// We need to add a boundary parameter to the header
var re = /^--([0-9A-Z'()+_,-./:=?]{1,70})[ \f\t\v\u00a0\u1680\u180e\u2000-\u200a\u2028\u2029\u202f\u205f\u3000\ufeff]*$/im;
var boundary = re.exec(data);
if (boundary !== null) {
contentType += '; boundary="' + boundary[1] + '"';
}
// Fix textarea.value EOL normalisation (multipart/form-data should use CR+NL, not NL)
data = data.replace(/\n/g, '\r\n');
}
} else {
contentType = form.attr('enctype') || form.attr('encoding')