Home > Software engineering >  Getting error while framing request URL using appscript
Getting error while framing request URL using appscript

Time:11-15

I am trying to frame request for API using appscript.

var url_string = "https://*.cognitiveservices.azure.com/vision/v3.2/describe"
  let body = {
    '"url"':'"https://www.khwaahish.com/wp-content/uploads/2022/01/khwaahish-white-bg-logo.jpg"'
    
    };
    const headers = { 
    'method' : 'POST',
    'Host':'imagealttextcreation.cognitiveservices.azure.com',
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key':'###',
    'payload': body
  }; 
  var response = UrlFetchApp.fetch(url_string,headers)
  Logger.log(response)

I am getting invalid request error. But the same thing is working when i try manually(attached image for same).

Am I missing something while forming this request in the appscript?

enter image description here

When tried manually using the browser the functionality works. i want help in correcting the request using appscript.

CodePudding user response:

From the official document, in your script, how about the following modification?

Modified script:

var url_string = "https://*.cognitiveservices.azure.com/vision/v3.2/describe";
let body = { url: "https://www.khwaahish.com/wp-content/uploads/2022/01/khwaahish-white-bg-logo.jpg" };
const options = {
  headers: { "Ocp-Apim-Subscription-Key": "###" }, // Please set your value.
  payload: JSON.stringify(body),
  contentType: "application/json"
};
var response = UrlFetchApp.fetch(url_string, options);
Logger.log(response.getContentText())

Reference:

  • Related