1

I have a list of URLs that all refer to images. I want to loop through the list and call a face recognition API that accepts these URLs. To call the API, I need to provide the payload dictionary. However, the example code from the API requires the following form for the payload dictionary:

payload = "{\"url\":\"https://inferdo.com/img/face-3.jpg\",\"accuracy_boost\":3}"

The URL in this example payload dictionary would look like this in my list:

list_of_urls = ["https://inferdo.com/img/face-3.jpg", ...]

How can I insert the entries of my list into the payload dictionary with a for loop?

I tried to use a "regular" payload dictionary, but it did not work:

for url_path in list_of_urls:
    payload = {'url' : url_path,'accuracy_boost':3}

1 Answer 1

3

I went to the API documentation and found you need to send the payload as JSON. Something like this will do the job:

import requests
import json

endpoints = {
    'face': 'https://face-detection6.p.rapidapi.com/img/face'
    'face_age_gender': 'https://face-detection6.p.rapidapi.com/img/face-age-gender'
}

urls = [
    'https://inferdo.com/img/face-3.jpg'
]

headers = {
    'x-rapidapi-host': 'face-detection6.p.rapidapi.com',
    'x-rapidapi-key': 'YOUR-API-KEY',
    'content-type': 'application/json',
    'accept': 'application/json'
}

for url in urls:
    payload = {
        'url': url,
        'accuracy_boost': 3
    }

    r = requests.post(
        endpoints.get('face'), # or endpoint.get('face_age_gender')
        data=json.dumps(payload),
        headers=headers
    )

    if r.ok:
        # do something with r.content or r.json()

I hope it helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.