Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/targets/python/requests/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ export const requests: Client<RequestsOptions> = {
payload = {};
postData.params.forEach(p => {
if (p.fileName) {
files[p.name] = `open('${p.fileName}', 'rb')`;
if (p.contentType) {
files[p.name] = `('${p.fileName}', open('${p.fileName}', 'rb'), '${p.contentType}')`;
} else {
files[p.name] = `('${p.fileName}', open('${p.fileName}', 'rb'))`;
}

hasFiles = true;
} else {
payload[p.name] = p.value;
Expand Down Expand Up @@ -96,7 +101,11 @@ export const requests: Client<RequestsOptions> = {
}

// The `open()` call must be a literal in the code snippet.
addPostProcessor(code => code.replace(/"open\('(.+)', 'rb'\)"/g, 'open("$1", "rb")'));
addPostProcessor(code =>
code
.replace(/"\('(.+)', open\('(.+)', 'rb'\)\)"/g, '("$1", open("$2", "rb"))')
.replace(/"\('(.+)', open\('(.+)', 'rb'\), '(.+)'\)"/g, '("$1", open("$2", "rb"), "$3")')
);
break;

default: {
Expand Down
2 changes: 1 addition & 1 deletion src/targets/python/requests/fixtures/multipart-data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

url = "https://httpbin.org/anything"

files = {"foo": open("src/fixtures/files/hello.txt", "rb")}
files = {"foo": ("src/fixtures/files/hello.txt", open("src/fixtures/files/hello.txt", "rb"), "text/plain")}
payload = {"bar": "Bonjour le monde"}

response = requests.post(url, data=payload, files=files)
Expand Down
2 changes: 1 addition & 1 deletion src/targets/python/requests/fixtures/multipart-file.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

url = "https://httpbin.org/anything"

files = {"foo": open("src/fixtures/files/hello.txt", "rb")}
files = {"foo": ("src/fixtures/files/hello.txt", open("src/fixtures/files/hello.txt", "rb"), "text/plain")}

response = requests.post(url, files=files)

Expand Down