I'm using the File Upload REST resource to upload an image.

The upload is working fine but my issue starts after the upload as the image appears broken

I tried to open the image in the browser, it showed a white outlined square on a black background.

I saved this black image and opened it in photoshop, the image displayed correctly

did anybody face such an issue?

Comments

mlulu created an issue. See original summary.

cilefen’s picture

Status: Active » Postponed (maintainer needs more info)

Please share an example image.

mlulu’s picture

StatusFileSize
new366.43 KB

HYG

mlulu’s picture

StatusFileSize
new82.7 KB
cilefen’s picture

Have you compared, say, with shasum, whether the image file was modified? Also, please share your code and the steps to reproduce. Thank you

mlulu’s picture

no, I didn't compare if the file was modified
I didn't make changes to the code, I followed the same steps mentioned here.

POST /file/upload/node/entity/field_image?_format=json HTTP/1.1
Content-Type: application/octet-stream
Content-Disposition: file; filename="filename.jpg"
Authorization: Bearer ................
[… binary file data …]
cilefen’s picture

Status: Postponed (maintainer needs more info) » Active
Issue tags: -Needs steps to reproduce
wim leers’s picture

Category: Bug report » Support request
Priority: Major » Normal
Status: Active » Postponed (maintainer needs more info)
Issue tags: +API-First Initiative

#6: I think @cilefen's was actually asking whether the file on your local machine that you uploaded matches the file that Drupal stored on the server.

In other words: are the MD5 hashes of both files the same?

mlulu’s picture

Thank you for the explanation @wim-leers, the MD5 of both files are different

I was searching and found the same issue explained here
https://drupal.stackexchange.com/questions/280941/how-to-correctly-forma... but I can't understand where to place the code

mlulu’s picture

hopefully, the information which I provided is enough, let me know if you need any more information

cilefen’s picture

Are you certain you are sending a binary representation of the file?

mlulu’s picture

StatusFileSize
new83.55 KB
new67.45 KB

the attached file guliyan-belgian-1024x290.jpg is the file before sending and guliyanbelgianchocolatecafe_0.jpg is the file after uploading with REST API

cilefen’s picture

That does not actually answer my question. How, technically, are you uploading? Is it curl? You haven’t given us enough information as yet.

mlulu’s picture

@cilefen, the development team are using Python requests to upload the images

kindly check below the code which they are using

def upload_image(image_path):
headers = {'Content-Type': 'application/octet-stream',
'Content-Disposition': 'file; filename="{}"'.format(image_name.replace('images/', '')),
'Authorization': 'Bearer ......................'}
image_filename = os.path.basename(image_path)
multipart_form_data = {'file': (image_filename, open(image_path, 'rb'))}
response = requests.post('localhost/file/upload/node/entity/field_image?_format=json',
files=multipart_form_data,
headers=headers)
return response.json()

mlulu’s picture

@cilefen, Is the information that I have provided enough?

cilefen’s picture

Yes. I think you have to double-check the documentation on the Python requests library. The example below works perfectly well with curl, and uploads an image without modifications. I think that requests is not sending a binary representation in #14:

curl -v -X POST -H 'Content-Type: application/octet-stream' -H 'Content-Disposition: file; filename="coin1.png"'  --user foo:bar --data-binary @coin1.png 'http://127.0.0.1:8080/drupal8x/file/upload/node/article/field_image?_format=json'
wim leers’s picture

+1 to what @cilefen said.

The reason I asked to compare the MD5 hashes was to check whether you were correctly uploading a file. If the MD5 hashes are different, then you're not uploading it correctly.

This is not really your fault, most of the tools for using HTTP requests with binary bodies do not have a great developer experience: they tend to be fairly confusing.

Try @cilefen's suggestion in #16! :)

mlulu’s picture

Thank you @cilefen @Wim
I tried the curl and it's working fine

wim leers’s picture

Status: Postponed (maintainer needs more info) » Fixed

Great! 🥳

wim leers’s picture

Crediting @cilefen & me.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

marcelovani’s picture

[Solved] I made this work by posting the actual content of the image rather than base64_encoding it...

I posted my solution and a snippet in PHP here https://drupal.stackexchange.com/questions/280941/how-to-correctly-forma...