Python 3 API Example

This example uses General Table OCR as an example to describe how to use Python 3 to call an API. To call other APIs, replace https://{endpoint}/v2/{project_id}/ocr/general-table in code with the actual one.

# encoding:utf-8

import requests
import base64

url = "https://{endpoint}/v2/{project_id}/ocr/general-table"
token = "Actual token value obtained by the user"
headers = {'Content-Type': 'application/json', 'X-Auth-Token': token}

imagepath = r'./data/general-table-demo.png'
with open(imagepath, "rb") as bin_data:
    image_data = bin_data.read()
image_base64 = base64.b64encode(image_data).decode("utf-8")  # Base64 encoding of images.
payload = {"image": image_base64}  # url or image.

response = requests.post(url, headers=headers, json=payload)
print(response.text)
Table 1 Parameters

Parameter

Description

url

API request URL, for example, https://{endpoint}/v2/{project_id}/ocr/general-table in this example.

token

A token is a user's access credential, which includes user identities and permissions. When you call an API to access a cloud service, a token is required for identity authentication.

For details about how to obtain a token, see Getting Started.

imagepath

Image path. An image file path or image URL is supported. The URL can be an HTTP/HTTPS or OBS URL.