1

I'm trying to use the resmush.it API from Apps Script and I'm struggling when making the request with UrlFetch.

From their documentation, I understand they need a files array, in a multipart/form-data request.

What I'm doing so far (the images come from a Google Slides)

function myFunction() {
  const OPTIMIZER_URL = "http://api.resmush.it/ws.php"
  let slides = SlidesApp.openById("1TUZSgG_XXX_ni6VhdbE5usRyMc");
  let pages = slides.getSlides();
  pages.forEach((page) => {
    let images = page.getImages();
    images.forEach(image => {
        let payload = {
          files: [{
            file: image.getBlob()
          }]
        }
        let options = {
          method: "POST",
          payload: payload,
          muteHttpExceptions : true
        }
        let response = UrlFetchApp.fetch(OPTIMIZER_URL, options)
        let jsonResponse = JSON.parse(response.getContentText())
        console.log(jsonResponse);
    })
  })
}

I also tried with payload = { file: image.getBlob() } but no success.

I get this error everytime:

{ error: 400,
  error_long: 'No file or url provided',
  generator: 'reSmush.it rev.3.0.4.20210124' }

Can you see what is wrong ?

Thank you

ValLeNain
  • 2,232
  • 1
  • 18
  • 37
  • According to the documentation the files should be binary files rather than blob. Here is a question about how to convert a blob into a binary https://stackoverflow.com/q/27208407/1595451. If you need further help, please show what you tried to do the conversion and add a brief description of your search efforts as is suggested in [ask]. – Rubén May 30 '22 at 19:05
  • By guessing the specification of the API you want to use, I proposed a modified script as an answer. Could you please confirm it? If I misunderstood the specification of the API, I apologize. – Tanaike May 31 '22 at 01:54

1 Answers1

1

Although I'm not sure whether from your provided document I could correctly understand the specification of the API you want to use, how about the following modified script?

When I saw your script, I'm worried that your image.getBlob() has no name. In that case, the data is not sent as files. I thought that this might be the reason for your issue. When my this guess is reflected in your script, it becomes as follows.

Modified script:

function myFunction() {
  const OPTIMIZER_URL = "http://api.resmush.it/ws.php"
  let slides = SlidesApp.openById("1TUZSgG_XXX_ni6VhdbE5usRyMc");
  let pages = slides.getSlides();
  pages.forEach((page) => {
    let images = page.getImages();
    images.forEach(image => {
      let options = {
        method: "POST",
        payload: { files: image.getBlob().setName("sample") },
        muteHttpExceptions: true
      }
      let response = UrlFetchApp.fetch(OPTIMIZER_URL, options)
      console.log(response.getContentText());
    })
  })
}
  • In this modification, the request body uses { files: image.getBlob().setName("sample") }. I'm not sure about the detailed specification of the API you want to use. So, if the key of files cannot be used, please modify files to file and test it again.

  • And, if your API is required to use the unique names of the uploaded files for each request, please set the unique names by modifying sample of image.getBlob().setName("sample").

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • I had to add the png extension in the `setName` function otherwise the API rejected it, but your solution worked! – ValLeNain May 31 '22 at 07:48
  • @ValLeNain Thank you for replying and testing it. I'm glad your issue was resolved. Thank you, too. – Tanaike May 31 '22 at 08:20