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

Content-Type missing in Python Requests code generated for file upload of image #247

Closed
benjose22 opened this issue May 15, 2020 · 6 comments · Fixed by #267
Closed

Content-Type missing in Python Requests code generated for file upload of image #247

benjose22 opened this issue May 15, 2020 · 6 comments · Fixed by #267

Comments

@benjose22
Copy link

Describe the bug
Content-Type is mandate while sending POST request for image upload. Content-Type is missing while generating Python Requests code using Postman code generator. E.g. 'image/png' hs to be included for uploading PNG files.

To Reproduce
In Body, set form-data.
Set KEY as 'upload' (note that this is not relevernt to the issue. I am just trying to match the screenshot.). Choose File from the dropdown(default is Text)
Set value - choose a png image from you computer.
Genererate Python Requests code.
Try executing the python code. We get error as invalid PNG file.

Expected code snippet and corresponding request
Expected-
files = [('upload', ('png_image.png', open('png_image.png', 'rb'), 'image/png'))]
Current -
files = [
('upload', open('/C:/fileupload/png_image.png','rb'))
]

Screenshots
image

image

@someshkoli
Copy link
Contributor

someshkoli commented May 17, 2020

Try running the code snippet obtained by turning off the new codegen mode in the settings.
image

and try to use that snippet again ? I'm getting diffrent code snippets
This is new codegen mode turned off:

import requests

url = "http:///upload"

payload = "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name=\"upload\"; filename=\"Screenshot from 2020-05-16 19-00-00.png\"\r\nContent-Type: image/png\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--"
headers = {
    'content-type': "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
    'cache-control': "no-cache",
    'Postman-Token': "b7e7c718-7cbc-4fd4-aca7-657f6ae3aa0c"
    }

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

print(response.text)`

New codegen mode turned on:

    import requests  

    url = "/upload"

    payload = {}
    files = {('upload': open('/home/wolf/Pictures/Screenshot from 2020-05-16 19-00-00.png','rb')}
    headers= {}

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

    print(response.text.encode('utf8'))

@Marti2203
Copy link

Just a quick nitpick as i am just checking out this issue because of a similar problem with the codegenerator for Python Requests. You have generated a GET request and not a POST, I do not think it is a big issue, but just informing as I saw the answer is recent and it would be good to fix it it before it creates more hassles

@benjose22
Copy link
Author

Wow! The old code generator works like a charm!

Do you want me close this issue or you guys are fixing in new code gen?

@Marti2203
Copy link

Currently me and @dasyad00 have made a pull request that may fix the new code generator issue as we also had some problems with it.

@someshkoli
Copy link
Contributor

Wow! The old code generator works like a charm!

Do you want me close this issue or you guys are fixing in new code gen?

Let it be here. The maintainers will close it.

@someshkoli
Copy link
Contributor

@benjose22 Can you tell me exactly which request was giving an error ? I am not able to reproduce it now ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants