1

I am working with an API for license plate recognition ; and i get this curl command :

How to implement such call with curl in PYTHON?

curl "https://api.havenondemand.com/1/api/async/recognizelicenseplates/v1?url=https%3A%2F%2Fwww.havenondemand.com%2Fsample-content%2Fvideos%2Fgb-plates.mp4&source_location=GB&apikey=695e513c-xxxx-xxxx-xxxx-xxxxxxxxxx"

curl -X POST --form "url=https://www.havenondemand.com/sample-content/videos/gb-plates.mp4" --form "source_location=GB" --form "apikey=695e513c-xxxx-xxxx-a666-xxxxxxxxxx" https://api.havenondemand.com/1/api/async/recognizelicenseplates/v1
rohit
  • 75
  • 1
  • 4
  • 11

3 Answers3

5

In Python, using the requests module is a much better option. Install it first:

pip install requests

Then do this:

import requests

API_URL = "https://api.havenondemand.com/1/api/async/recognizelicenseplates/v1"

data = {
    "url": "https://www.havenondemand.com/sample-content/videos/gb-plates.mp4",
    "source_location": "GB",
    "apikey": "695e513c-xxxx-xxxx-a666-xxxxxxxxxx"
 }

response = requests.post(API_URL, data)
print(response.json())

Basically, any form fields should go inside the data dictionary as key value pairs. We use requests.post() function here. The function takes the target URL as the first parameter. And the form values as the second parameter.

We get a response object back. You can see the raw response by printing out the value of response.content. However, if you know that the response is JSON, you can use the json() method to parse the response and get back Python data type (dictionary).

masnun
  • 11,635
  • 4
  • 39
  • 50
0

There are multiple options. You could start with urllib2 (or any other HTTP library like requests). A better option could be to directly use the python client library for havenondemand

0

The fastest way to call Haven OnDemand APIs is to use HPE official libraries. You can install the HOD Python lib and use it as follows:

install the lib

pip install havenondemand

# put these in your file
from havenondemand.hodclient import *
from havenondemand.hodresponseparser import *

client = HODClient("API_KEY", version="v1")
parser = HODResponseParser()

# callback function
def asyncRequestCompleted(response):
    jobID = parser.parse_jobid(response)
    if jobID is None:
        errorObj = hodParser.get_last_error()
        for err in errorObj.errors:
            print ("Error code: %d \nReason: %s \nDetails: %s\n" % (err.error,err.reason, err.detail))
    else:
        client.get_job_status(jobID, requestCompleted)

def requestCompleted(response):
    payloadObj = parser.parse_payload(response)
    resp = ""
    if payloadObj is None:
        errorObj = parser.get_last_error()
        for err in errorObj.errors:
            if err.error == ErrorCode.QUEUED:
                time.sleep(2)
                client.get_job_status(err.jobID, requestCompleted)
                return
            elif err.error == ErrorCode.IN_PROGRESS:
                time.sleep(10)
                client.get_job_status(err.jobID, requestCompleted)
                return
            else:
                resp += "Error code: %d \nReason: %s \nDetails: %s\n" % (err.error,err.reason)
    else:
        print(payloadObj)

params = {}
params['url'] = 'https://www.havenondemand.com/sample-content/videos/gb-plates.mp4'
params['source_location'] = 'GB'
hodApp = HODApps.RECOGNIZE_LICENSE_PLATE
client.post_request(params, hodApp, True, callback=asyncRequestCompleted)
Community
  • 1
  • 1
Paco Vu
  • 211
  • 1
  • 3