Closed (fixed)
Project:
Drupal core
Version:
8.7.x-dev
Component:
rest.module
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Issue tags:
Reporter:
Created:
5 Sep 2019 at 09:43 UTC
Updated:
24 Sep 2020 at 15:19 UTC
Jump to comment: Most recent, Most recent file
Comments
Comment #2
cilefen commentedPlease share an example image.
Comment #3
mlulu commentedHYG
Comment #4
mlulu commentedComment #5
cilefen commentedHave you compared, say, with shasum, whether the image file was modified? Also, please share your code and the steps to reproduce. Thank you
Comment #6
mlulu commentedno, I didn't compare if the file was modified
I didn't make changes to the code, I followed the same steps mentioned here.
Comment #7
cilefen commentedComment #8
wim leers#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?
Comment #9
mlulu commentedThank 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
Comment #10
mlulu commentedhopefully, the information which I provided is enough, let me know if you need any more information
Comment #11
cilefen commentedAre you certain you are sending a binary representation of the file?
Comment #12
mlulu commentedthe attached file guliyan-belgian-1024x290.jpg is the file before sending and guliyanbelgianchocolatecafe_0.jpg is the file after uploading with REST API
Comment #13
cilefen commentedThat does not actually answer my question. How, technically, are you uploading? Is it curl? You haven’t given us enough information as yet.
Comment #14
mlulu commented@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()
Comment #15
mlulu commented@cilefen, Is the information that I have provided enough?
Comment #16
cilefen commentedYes. 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:
Comment #17
wim leers+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! :)
Comment #18
mlulu commentedThank you @cilefen @Wim
I tried the curl and it's working fine
Comment #19
wim leersGreat! 🥳
Comment #20
wim leersCrediting @cilefen & me.
Comment #22
marcelovani[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...