简体   繁体   中英

Django http request to api error

As this is the first time I'm trying this out, I do not know what is wrong with the problem. So it would be great if someone can help me solve this problem

The code I'm using is at the bottom page of this website: https://www.twilio.com/blog/2014/11/build-your-own-pokedex-with-django-mms-and-pokeapi.html

Where it give example on how you can make HTTP request function and retrieve database on your query.

The code on the website is this.

query.py

import requests
import json

BASE_URL = 'http://pokeapi.co'


def query_pokeapi(resource_url):
    url = '{0}{1}'.format(BASE_URL, resource_url)
    response = requests.get(url)

    if response.status_code == 200:
        return json.loads(response.text)
    return None


charizard = query_pokeapi('/api/v1/pokemon/charizard/')

sprite_uri = charizard['sprites'][0]['resource_uri']
description_uri = charizard['descriptions'][0]['resource_uri']

sprite = query_pokeapi(sprite_uri)
description = query_pokeapi(description_uri)

print
charizard['name']
print
description['description']
print
BASE_URL + sprite['image']

In my edit, I only change these print line at the bottom of this

query.py

print(charizard['name'])
print(description['description'])
print(BASE_URL + sprite['image'])

But i got this error instead

Traceback (most recent call last): File "query2.py", line 46, in sprite_uri = charizard['sprites'][0]['resource_uri'] TypeError: 'NoneType' object is not subscriptable

query_pokeapi must be returning None, which would mean that your API call is not receiving a 200 HTTP response. I'd check your URL, to make sure it's properly formed. Test it in your web browser.

best practice would be to try-except your API call with an error message letting you know that your API call failed and otherwise routing the thread.

Update: reread and the sub scripting issue could be in any layer of your nested object.

Evaluate charizard['sprites'][0]['resource_uri'] step by step in your debugger.

When you call api requests.get(url) then its response is

More than one resource is found at this URI

you are using charizard['sprites'][0]['resource_uri'] on result and it's raising exception.

When I tried to get response then status code is 300 so

def query_pokeapi(resource_url) returning None value.

'{0}{1}'.format(BASE_URL, resource_url)

Update

it means at {0} BASE_URL will be places and at {1} resource_url will be places.

Complete url will be

url = '{0}{1}'.format(BASE_URL, resource_url)
url = 'http://pokeapi.co/api/v1/pokemon/charizard/'.

update

you can try

import json
charizard = query_pokeapi('/api/v1/pokemon/')
data = json.loads(charizard.content)
print data['objects'][0]['descriptions'][0]

result will be

{u'name': u'ekans_gen_1', u'resource_uri': u'/api/v1/description/353/'}

Update with complete code

import requests
import json

BASE_URL = 'http://pokeapi.co'


def query_pokeapi(resource_url):
    url = '{0}{1}'.format(BASE_URL, resource_url)
    response = requests.get(url)

    if response.status_code == 200:
        return json.loads(response.text)
    return None

charizard = query_pokeapi('/api/v1/pokemon/')
print charizard['objects'][0]['descriptions'][0]

result will be:

{u'name': u'ekans_gen_1', u'resource_uri': u'/api/v1/description/353/'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM