jackm / kijiji-manager

App for viewing, posting, reposting, and deleting your Kijiji ads
https://pypi.org/project/kijiji-manager/
MIT License
52 stars 11 forks source link

Stupid question about image uploading and image headers #58

Closed anonwhitemouse closed 1 year ago

anonwhitemouse commented 1 year ago

I am trying to use the upload_image() function manually (without Flask forms or the Flask server, etc) and I keep getting an error "The request to upload an image was not formatted correctly.".

I suspect this is due to my lack of understanding of the three values (data.filename, data.read(), and data.content_type) in the multipart form data section "files = { ... }" of the kijijiapi.py file, around line 250-ish.

1) I assume the data.filename is the local name of the image file I want to upload, as a string, like "myimage.jpg" 2) I assume data.read() is just an object containing a binary read() of my local image file, which I have done with: with io.open("myimage.jpg", rb, newline=None) as f: image = f.read() and then passed that exact image object into that files dict 3) I don't understand the third field regarding the content_type -- I assumed it should be "image/jpeg" but that still throws the error. If I am wrong, what should this be, and are my assumptions correct or am I off the mark wildly here?

I guess what I am asking is: how am I supposed to know what format the request expects such that it can be sent along with files in order to retrieve the necessary image URLs?

Thanks

As a side note: All of the rest of the Flask and server stuff works properly (except when I try to manually post an ad, then the ad goes up without its image).

jackm commented 1 year ago

The data argument to this method is a werkzeug.FileStorage type.

The third field in tuple for the file key is the file MIME type which depends on the file type being sent. For JPEG images it will be image/jpeg, for PNG images it will be image/png, etc. It may also have a charset parameter appended onto it as well, e.g. image/jpeg; charset=utf-8. This MIME type string becomes the value for the Content-Type header for the HTTP PUT request.

anonwhitemouse commented 1 year ago

Hey Jack, thank you for your response, I definitely understand the third field stuff a lot better now.