Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix browsable API not supporting multipart/form-data correctly #5176

Merged
merged 4 commits into from Jun 16, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions rest_framework/static/rest_framework/js/ajax-form.js
Expand Up @@ -37,6 +37,21 @@ 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
// We assume the first valid-looking boundary line in the body is correct
// regex is from RFC 2046 appendix A
var boundaryCharNoSpace = "0-9A-Z'()+_,-./:=?";
var boundaryChar = boundaryCharNoSpace + ' ';
var re = new RegExp('^--([' + boundaryChar + ']{0,69}[' + boundaryCharNoSpace + '])[\\s]*?$', 'im');
var boundary = data.match(re);
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')

Expand Down