Fetching Json data from API

Maybe this code snippet helps. I use executeScript to call the free OCR API from inside the RPA software. This approach works for any API.

For generating the Javascript code I used the PostMan code export feature. So if your api call works from inside Postman, than you can export the working code.

{
  "Name": "ocr api",
  "CreationDate": "2023-4-29",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://forum.ui.vision",
      "Value": "",
      "Description": "Open any website, we need a place to run \"executeScript\" inside"
    },
    {
      "Command": "executeScript",
      "Target": "async function callOCR() {\nconst apiKey = \"helloworld\";\nconst imageurl = \"https://ui.vision/Content/Images/ui.vision.logo2.webp\";\nconst ocrUrl = \"https://api.ocr.space/parse/image\";\n\nvar myHeaders = new Headers();\nmyHeaders.append(\"apikey\", apiKey);\n\nvar formdata = new FormData();\nformdata.append(\"language\", \"ger\");\nformdata.append(\"ocrengine\", \"2\");\nformdata.append(\"url\", imageurl);\n\nvar requestOptions = {\n  method: 'POST',\n  headers: myHeaders,\n  body: formdata,\n  redirect: 'follow'\n};\n\nconst response = await fetch (ocrUrl, requestOptions)\n\nconst data = await response.json();\n\nparsed_text = data[\"ParsedResults\"][0][\"ParsedText\"]\n\nreturn JSON.stringify(parsed_text);\n\n}\nreturn callOCR();",
      "Value": "v",
      "Description": "Call OCR API, code was generated by Postman"
    },
    {
      "Command": "echo",
      "Target": "OCR Text result=${v}",
      "Value": "green",
      "Description": ""
    }
  ]
}

Postman code export:

1 Like