Search code examples
azure-cognitive-services

What is the Correct Endpoint for the Form Recognizer API?


Docs about using the Azure's Form Recognizer seem unclear. What is the correct ENDPOINT to send my request?

I'm using python and have followed the docs to use the prebuilt receipt model of Form Recognizer but don't get the expected output. I'm unsure if I'm using the correct endpoint. I tried two things:

  1. Reading this tutorial, it is stated that I need to look up the <ENDPOINT> in my resource's overview page. In my case it is: formextractor.cognitiveservices.azure.com. So I tried this:
import http.client, urllib.request, urllib.parse, urllib.error, base64

params = urllib.parse.urlencode({
    })

# key = '...'
headers = {
    # Request headers
    'Content-Type': 'application/json',
    'Ocp-Apim-Subscription-Key': key,
}

source = r"https://www.w3.org/WAI/WCAG20/Techniques/working-examples/PDF20/table.pdf"
body = {"url":source}
body = json.dumps(body)

try:
    conn = http.client.HTTPSConnection('formextractor.cognitiveservices.azure.com')
    conn.request("POST", "/formrecognizer/v1.0-preview/prebuilt/receipt/asyncBatchAnalyze?s" % params, f"{body}", headers)
    response = conn.getresponse()
    data = response.read()
    operationURL = "" + response.getheader("Operation-Location")
    print ("Operation-Location header: " + operationURL)
    conn.close()
except Exception as e:
    print(e)

This returns:

[Errno 8] nodename nor servname provided, or not known
  1. But in the API Docs the ENDPOINT is already fixed to westeurope.api.cognitive.microsoft.com, which is where my resource is located. So I tried this:
# ... same headers, body and params as before
try:
    conn = http.client.HTTPSConnection('westeurope.api.cognitive.microsoft.com')
    conn.request("POST", "/formrecognizer/v1.0-preview/prebuilt/receipt/asyncBatchAnalyze?%s" % params, f"{body}", headers)
    response = conn.getresponse()
    data = response.read()
    operationURL = "" + response.getheader("Operation-Location")
    print ("Operation-Location header: " + operationURL)
    conn.close()
except Exception as e:
    print("[Errno {0}] {1}".format(e.errno, e.strerror))

Outputs an URL where I see:

{"error":{"code":"401","message": "Access denied due to invalid subscription key or wrong API endpoint. Make sure to provide a valid key for an active subscription and use a correct regional API endpoint for your resource."}}

I am certain I'm using the correct key. But in any case neither of the two seems to work. Could you help me? Thank you.


Solution

  • It looks like the initial documentation you are mentioning is misleading. You can find the endpoint of your resource in Azure portal, in the resource overview. Sample for mine:

    Resource overview

    Form Recognizer API is (at the time of writing this answer) hosted in the following Azure regions:

    • West US 2 - westus2.api.cognitive.microsoft.com
    • West Europe - westeurope.api.cognitive.microsoft.com

    So in my case it's WestEurope, and as you mentioned it is the same on your resource. Once you got it, you then got a 401 Unauthorized

    Several possibilities:

    • You made an error in the way you are passing the header (wrong key name, wrong key value), but it looks okay based on your code above (but I'm not used to Python)
    • Your resource is not from the same region as the endpoint you are querying (please double check)
    • You are using the right root, but something is wrong in how you are calling it

    Once you have checked your region / key values, can you remove your ?%s"%params from your query? The Analyze Receipt method don't have params in query string (given documentation)