postman-app-support icon indicating copy to clipboard operation
postman-app-support copied to clipboard

Code Snippet > Python - Requests > Wrong 'Content-Type'

Open JanHusarcik opened this issue 1 year ago • 0 comments
trafficstars

Is there an existing issue for this?

  • [X] I have searched the tracker for existing similar issues and I know that duplicates will be closed

Describe the Issue

Hi team,

if I generate a code snippet for Python - Requests for a multipart POST request, a code such as this is generated:

import requests

url = "https://example.com"

payload = {'foob': 'bar'}
files=[
  ('file',('file.html','rb'),'text/html'))
]
headers = {
  'Content-Type': 'multipart/form-data',
  'Accept': 'application/json',
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)

note the 'Content-Type' value. However, Python's Request module need to generate the 'Content-Type' itself, including the boundary.

See https://stackoverflow.com/a/12385661 for details.

Regards, Jano

Steps To Reproduce

  1. Create new (working) multipart POST request.
  2. Generate code snippet for Python - Requests.
  3. Run the code using e.g. python 3.10.10 and requests 2.31.0
  4. request will fail (likely with error 500)
  5. review the python's request headers (you can include print(response.request.headers) on the last line to dump request's headers)

Screenshots or Videos

No response

Operating System

Windows

Postman Version

10.24.24

Postman Platform

Postman App

User Account Type

Signed In User

Additional Context?

No response

JanHusarcik avatar Apr 26 '24 10:04 JanHusarcik