Posting image in cell, work with cURL command, but does not in Python

183 Views Asked by At

When I use this :

curl --ssl-no-revoke -H "Authorization: Bearer $token$" -H "Content-Type: image/jpeg" -H "Content-Disposition: attachment; filename='IMG_6805.JPG'"-H "Content-Length: 9930" -X POST "https://api.smartsheet.com/2.0/sheets/$sheetID$/rows/$rowID$/columns/$columnID$/cellimages?altText=test" --data-binary @IMG_6805.JPG

I get that response:

curl: (3) URL using bad/illegal format or missing URL

followed by :

{"message":"SUCCESS","resultCode":0,"result":[{"id":3118469427160964,"rowNumber":1,"expanded":true,"createdAt":"2022-05....etc

and the image appear in the cell.

But if I use

import requests

headers = {
    'Authorization': 'Bearer %token%',
    'Content-Type': 'image/jpeg',
    'Content-Disposition': 'attachment; filename=\'%picfile%\'',
    # 'Content-Length': 'FILE_SIZE',
}

params = {
    'altText': 'test',
}

with open('%picfile%', 'rb') as f:
    data = f.read().replace(b'\n', b'')

response = requests.post('https://api.smartsheet.com/2.0/sheets/%sheetid%/rows/%rowid%/columns/%columnid%/cellimages', params=params, headers=headers, data=data)

I get the following error

ErrorCode:5463
Message: An error occurred while trying to process our image, It may be corrupt, too large to process, or an unsupported format.

Here is the code

import requests

f='IMG_6763.JPG'


sheetid = '7609187174770564'


headersPost= {
    'Authorization': 'Bearer $token$',
    'Content-Type': 'image/jpeg',
    'Content-Disposition': 'attachment; filename=\''+f+'\'' ,
    'Content-Lenght':'FILE_SIZE',
}    
params={
    'altText': 'lien'
}
print(headersPost)

print(params)

with open(f,'rb') as p:
    data=p.read().replace(b'\n',b'')


rpost=requests.post('https://api.smartsheet.com/2.0/sheets/7609187174770564/rows/3118469427160964/columns/5324051199616900/cellimages',params=params,headers=headersPost,data=data,stream=True)

print(rpost.text)
#rpost.raise_for_status()

The goal is to post in the columns a picture taht is on my computeur. I don't want to attach the file to the row (it is actually already attached). I want to see the picture in a cell so it gives a preview of what is attached to the row.

0

There are 0 best solutions below