`Bad Value` when connecting to an external API in google app script

108 views Asked by At

Trying to fetch data from the ConvertAPI's web to jpg but the program throws an error for a Bad Value. Here's my code :

const url = 'https://v2.convertapi.com/convert/web/to/png?Secret=...'
  const prams = [
    {
      "Name": "Url",
      "Value": "..."
    }
  ];

  Logger.log(UrlFetchApp.getRequest(url, prams));
1

There are 1 answers

3
Tanaike On

I believe your goal is as follows.

  • From your provided document, you want to convert the following HTTP request to Google Apps Script.

      POST https://v2.convertapi.com/convert/web/to/jpg?Secret=<YOUR SECRET HERE>
      Content-Type: application/json
    
      {
          "Parameters": [
              {
                  "Name": "Url",
                  "Value": ""
              },
              {
                  "Name": "StoreFile",
                  "Value": true
              }
          ]
      }
    

Unfortunately, your prams cannot be directly used for UrlFetchApp. And, UrlFetchApp.getRequest doesn't request. And also, your prams is different from the sample of the document you provided.

When these points are reflected in Google Apps Script, how about the following modification?

Modified script:

function myFunction() {
  const url = 'https://v2.convertapi.com/convert/web/to/jpg?Secret=<YOUR SECRET HERE>';
  const prams = {
    "Parameters": [
      {
        "Name": "Url",
        "Value": ""
      },
      {
        "Name": "StoreFile",
        "Value": true
      }
    ]
  };
  const options = {
    contentType: "application/json",
    payload: JSON.stringify(prams),
  };
  const res = UrlFetchApp.fetch(url, options);
  console.log(res.getContentText());
}
  • If an error occurs, please confirm your Secret and the values of prams again.

Note:

  • In the document, there is a sample curl command of curl -F "Url=" -F "StoreFile=true" https://v2.convertapi.com/convert/web/to/jpg?Secret=<YOUR SECRET HERE>. When this is converted to Google Apps Script, it becomes as follows.

      const url = "https://v2.convertapi.com/convert/web/to/jpg?Secret=<YOUR SECRET HERE>";
      const options = { payload: { "StoreFile": "true", "Url": "" } };
      const res = UrlFetchApp.fetch(url, options);
      console.log(res.getContentText());
    
  • Please test the above 2 patterns.

References: