Upload image to DatoCMS using Zoho Deluge (edited title)

Hello,

I’m trying to upload images via https to DatoCMS (using content management API), All went good!
But, finally the uploaded image files are getting corrupted
MY CODE:

/////////////////// Getting the Image by a GET Call //////////////////////////////////////////////////////////////
urlLink = "https://creator.zoho.eu/api/v2/Form_Report/70154005/File_upload/download";
getFile = invokeurl
[
	url :urlLink
	type :GET
	connection:"zcreator"
];
info "getFile: " + getFile;
// // //////////////////////////////////////// DatoCMS Create Upload API Starts Here ///////////////////////////////
data = {"data":{"type":"upload_request","attributes":{"filename":"clickHere.png"}}};
// // //////////////////////////// Step - 1 //////////////////////////////
API_Key = "**************************************";
headMap = Map();
headMap.put("X-Environment","initial-update");
headMap.put("X-Api-Version",3);
headMap.put("Authorization","Bearer " + API_Key);
headMap.put("Accept","application/json");
headMap.put("Content-Type","application/vnd.api+json");
////////////////////////////////////
requestAnUpload = invokeurl
[
	url :"https://site-api.datocms.com/upload-requests"
	type :POST
	parameters:data.toString()
	headers:headMap
];
//info "requestAnUpload: " + requestAnUpload;
attributes = requestAnUpload.getJSON("data");
forStepThree = attributes.getJSON("id");
attributes = attributes.getJSON("attributes");
awsUrl = attributes.getJSON("url");
info "awsUrl: " + awsUrl;
info "forStepThree: " + forStepThree;
// ///////////////////////////////////// step - 2 //////////////////////////////
hm = Map();
hm.put("content-type","application/octet-stream");
//////////////////// PUT Api Call /////////////////////////
uploadFile = invokeUrl(awsUrl,"PUT",getFile,hm,false);
info "uploadFile: " + uploadFile;
////////////////////////// Step - 3 //////////////////////////////////////
sThreeData = {"data":{"type":"upload","attributes":{"path":forStepThree,"author":"VenkatZoho","copyright":"2023 Haylo","default_field_metadata":{"en":{"alt":"IppoParu","title":"Sunny","custom_data":{"null":null}}}}}};
//////////////////////////// 
stepThreeRep = invokeurl
[
	url :"https://site-api.datocms.com/uploads"
	type :POST
	parameters:sThreeData.toString()
	headers:headMap
];
info "stepThreeRep: " + stepThreeRep;
forStepFour = stepThreeRep.getJSON("data");
forStepFour = forStepFour.getJSON("id");
info "forStepFour: " + forStepFour;
//////////////////////////////// Step - 4 ///////////////////////////////////////
fourUrl = "https://site-api.datocms.com/job-results/f66b19a78493a5de0dfad2f4";
getStepFour = invokeurl
[
	url :fourUrl
	type :GET
	headers:headMap
];
info "getStepFour: " + getStepFour;
return "";

Images:

Hey @venkateshwaran,

Welcome to the Dato forums! Sorry about the issue you’re running into.

It looks like this script is written in Zoho Deluge, which isn’t a scripting language we normally work with. Does it give you any detailed error logs when you attempt to make the request?

I tried to debug your script in their IDE, but unfortunately they have a daily limit for function invocations and I ran out for today before being able to fix the problem.

Have you tried reaching out to Zoho support to see if they can help you troubleshoot this? Normally, we on the Dato side support our first-party clients (like the JS one) and generic REST API calls. It’s hard for me to debug what Zoho is doing unless you can provide some logs and debug data.

Some things to check, though (just guessing here):

  • Can you provide a proper content-type, something like image/png or similar instead of application/octet-stream? You can also get it from the original image like contentType = getFile.get("responseHeader").get("content-type") ;
  • Can you provide a file extension, like clickhere.png instead of clickhere?
  • Does the S3 upload step work? Do you get a 202 OK from it, and can you access the image? Have you tried accessing the URL of that image directly after it’s uploaded, and if so, what does it look like?
  • Have you tried adding response-format: FILE to the getFile request?
  • (Incomplete investigation) It looks to me like the downloaded PDF sometimes has an invalid header (in plaintext). I was debugging this when I reached their limit, unfortunately, so couldn’t conclude this investigation.

Sorry I don’t have a full solution for you yet. I’ve contacted Zoho support to see if they can increase my webhooks limit, and if they do, I can keep trying to debug this with you. In the meantime, please check the above and also reach out to their support to see if they have any ideas.

@venkateshwaran, after some more debugging in Zoho Creator, I’m hoping this will help? Please try replacing (right before step 3):

uploadFile = invokeUrl(awsUrl,"PUT",getFile,hm,false);

with

uploadFile = invokeurl
[
		url: awsUrl
		type :PUT
		content-type: "application/octet-stream"
		parameters: getFile
];

I think you have to specify the content-type parameter directly, instead of passing along the hm map. I’m not sure why this is; you may wish to talk to Zoho about it. But at least that lets me put a usable file onto S3. Without that, Zoho was sending the wrong content type to S3 and causing file corruption by injecting an invalid header into the PNG file.

Please let me know if that helps at all, or if you have any further information that can help us troubleshoot. Thanks!

Hello @roger

Thanks for you idea, I tried the same in begining but it wasn’t worked but now it’s working great!

Finally the media files are now uploaded without an issue in DatoCMS server.

uploadFile = invokeurl
[
		url: awsUrl
		type :PUT
		content-type: "application/octet-stream"
		parameters: getFile
];
1 Like

Wonderful, thanks for confirming that worked!

1 Like