2

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));
Rubén
  • 34,714
  • 9
  • 70
  • 166
Vayun
  • 95
  • 1
  • 10

1 Answers1

0

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • This just gave me an http error – Vayun Nov 14 '22 at 15:26
  • @Vayun Thank you for replying. I have to apologize for my poor English skill. Unfortunately, I cannot understand your current situation from only `This just gave me an http error`. I think that this is due to my poor English skill. I deeply apologize for my poor English skill. In order to correctly understand your current issue, can you provide detailed information about it? By this, I would like to confirm it. If you can cooperate to resolve your issue, I'm glad. Can you cooperate to do it? – Tanaike Nov 15 '22 at 00:02
  • 1
    @Vayun please read the body of the response and let us know what error do you get. Tanaike posted a working solution for you. – Tomas Nov 21 '22 at 07:46