Skip to content

Commit 382513d

Browse files
authored
Merge pull request #267 from someshkoli/fix/pythonFormData
Adds ContentType to python snippets for multipart/formdata
2 parents d26d51e + e33ccec commit 382513d

File tree

2 files changed

+90
-5
lines changed

2 files changed

+90
-5
lines changed

codegens/python-requests/lib/util/parseBody.js

+84-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,78 @@
11
var _ = require('../lodash'),
2-
sanitize = require('./sanitize').sanitize;
2+
sanitize = require('./sanitize').sanitize,
3+
contentTypeHeaderMap = {
4+
'aac': 'audio/aac',
5+
'abw': 'application/x-abiword',
6+
'arc': 'application/x-freearc',
7+
'avi': 'video/x-msvideo',
8+
'azw': 'application/vnd.amazon.ebook',
9+
'bin': 'application/octet-stream',
10+
'bmp': 'image/bmp',
11+
'bz': 'application/x-bzip',
12+
'bz2': 'application/x-bzip2',
13+
'csh': 'application/x-csh',
14+
'css': 'text/css',
15+
'csv': 'text/csv',
16+
'doc': 'application/msword',
17+
'docx': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
18+
'eot': 'application/vnd.ms-fontobject',
19+
'epub': 'application/epub+zip',
20+
'gif': 'image/gif',
21+
'htm': 'text/html',
22+
'html': 'text/html',
23+
'ico': 'image/vnd.microsoft.icon',
24+
'ics': 'text/calendar',
25+
'jar': 'application/java-archive',
26+
'jpeg': 'image/jpeg',
27+
'jpg': 'image/jpeg',
28+
'js': 'text/javascript',
29+
'json': 'application/json',
30+
'jsonld': 'application/ld+json',
31+
'mid': 'audip/midi',
32+
'midi': 'audio/midi',
33+
'mjs': 'text/javascript',
34+
'mp3': 'audio/mpeg',
35+
'mpeg': 'video/mpeg',
36+
'mpkg': 'application/vnd.apple.installer+xml',
37+
'odp': 'application/vnd.oasis.opendocument.presentation',
38+
'ods': 'application/vnd.oasis.opendocument.spreadsheet',
39+
'odt': 'application/vnd.oasis.opendocument.text',
40+
'oga': 'audio/ogg',
41+
'ogv': 'video/ogg',
42+
'ogx': 'application/ogg',
43+
'otf': 'font/otf',
44+
'png': 'image/png',
45+
'pdf': 'application/pdf',
46+
'ppt': 'application/vnd.ms-powerpoint',
47+
'pptx': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
48+
'rar': 'application/x-rar-compressed',
49+
'rtf': 'application/rtf',
50+
'sh': 'application/x-sh',
51+
'svg': 'image/svg+xml',
52+
'swf': 'application/x-shockwave-flash',
53+
'tar': 'application/x-tar',
54+
'tif': 'image/tiff',
55+
'tiff': 'image/tiff',
56+
'ts': 'video/mp2t',
57+
'ttf': 'font/ttf',
58+
'txt': 'text/plain',
59+
'vsd': 'application/vnd.visio',
60+
'wav': 'audio/wav',
61+
'weba': 'audio/webm',
62+
'webm': 'video/webm',
63+
'webp': 'image/webp',
64+
'woff': 'font/woff',
65+
'woff2': 'font/woff2',
66+
'xhtml': 'application/xhtml+xml',
67+
'xls': 'application/vnd.ms-excel',
68+
'xlsx': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
69+
'xml': 'text/xml',
70+
'xul': 'application/vnd.mozilla.xul+xml',
71+
'zip': 'application/zip',
72+
'3gp': 'video/3gpp',
73+
'7z': 'application/x-7z-compressed',
74+
'7-zip': 'application/x-7z-compressed'
75+
};
376

477
/**
578
* Used to parse the body of the postman SDK-request and return in the desired format
@@ -64,7 +137,16 @@ module.exports = function (request, indentation, bodyTrim) {
64137
`'${sanitize(value.value, request.body.mode, bodyTrim)}'`);
65138
});
66139
bodyFileMap = _.map(_.filter(enabledBodyList, {'type': 'file'}), function (value) {
67-
return `${indentation}('${value.key}', open('${sanitize(value.src, request.body.mode, bodyTrim)}','rb'))`;
140+
var filesrc = value.src,
141+
fileType = filesrc.split('.')[filesrc.split('.').length - 1],
142+
contentType = contentTypeHeaderMap[fileType];
143+
144+
if (!contentType) {
145+
contentType = 'application/octet-stream';
146+
}
147+
return `${indentation}('${value.key}',('${filesrc.split('/')[filesrc.split('/').length - 1]}'` +
148+
`,open('${sanitize(filesrc, request.body.mode, bodyTrim)}','rb'),` +
149+
`'${contentType}'))`;
68150
});
69151
requestBody = `payload={${bodyDataMap.join(',\n')}}\nfiles=[\n${bodyFileMap.join(',\n')}\n]\n`;
70152
}

codegens/python-requests/test/unit/converter.test.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,12 @@ describe('Python- Requests converter', function () {
106106
expect.fail(null, null, error);
107107
}
108108
expect(snippet).to.be.a('string');
109-
expect(snippet).to.include('(\'no file\', open(\'/path/to/file\',\'rb\'))');
110-
expect(snippet).to.include('(\'no src\', open(\'/path/to/file\',\'rb\'))');
111-
expect(snippet).to.include('(\'invalid src\', open(\'/path/to/file\',\'rb\'))');
109+
// eslint-disable-next-line max-len
110+
expect(snippet).to.include('(\'no file\',(\'file\',open(\'/path/to/file\',\'rb\'),\'application/octet-stream\'))');
111+
// eslint-disable-next-line max-len
112+
expect(snippet).to.include('(\'no src\',(\'file\',open(\'/path/to/file\',\'rb\'),\'application/octet-stream\'))');
113+
// eslint-disable-next-line max-len
114+
expect(snippet).to.include('(\'invalid src\',(\'file\',open(\'/path/to/file\',\'rb\'),\'application/octet-stream\'))');
112115
});
113116
});
114117

0 commit comments

Comments
 (0)