对api的Django HTTP请求

2024-06-09 06:57:39 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直在努力让这个工作,但同时我不明白其中一些代码的含义。我很抱歉问了这么久,但我想了解这些是如何工作的。在

我正在尝试使用django向另一个API发出HTTP请求来执行POST-and-GET方法。基于网站代码示例,这是url:https://www.twilio.com/blog/2014/11/build-your-own-pokedex-with-django-mms-and-pokeapi.html

由于我想在我的API上使用HTTP请求来调用其他API,因此我想更好地理解这些API是如何工作的以及如何使用它。在

代码在网站的底部。但我会在这里提供代码,这样你就更容易了。在

网站代码

from django_twilio.views import twilio_view
from twilio.twiml import Response
import requests
import json

BASE_URL = 'http://pokeapi.co'

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

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

@twilio_view
def incoming_message(request):
    twiml = Response()

    body = request.POST.get('Body', '')
    body = body.lower()

    pokemon_url = '/api/v1/pokemon/{0}/'.format(body)
    pokemon = query_pokeapi(pokemon_url)

    if pokemon:
        sprite_uri = pokemon['sprites'][0]['resource_uri']
        description_uri = pokemon['descriptions'][0]['resource_uri']

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

        message = '{0}, {1}'.format(pokemon['name'], description['description'])
        image = '{0}{1}'.format(BASE_URL, sprite['image'])

        frm = request.POST.get('From', '')
        if '+44' in frm:
            twiml.message('{0} {1}'.format(message, image))
            return twiml
        twiml.message(message).media(image)
        return twiml

    twiml.message("Something went wrong! Try 'Pikachu' or 'Rotom'")
    return twiml

我的问题是:

  1. 我读过request.POSTrequest.POST.get,但我还是不明白。request.POST=POST方法/create函数不是吗?

  2. body.lower是什么意思?似乎找不到任何关于它的东西。

  3. 我对这部分很困惑

    sprite_uri = pokemon['sprites'][0]['resource_uri']
    description_uri = pokemon['descriptions'][0]['resource_uri']
    
    sprite = query_pokeapi(sprite_uri)
    description = query_pokeapi(description_uri)
    

pokemon['sprites']引用api中的sprites字段吗?在

  1. 这到底意味着什么?在

        frm = request.POST.get('From', '')
        if '+44' in frm:
            twiml.message('{0} {1}'.format(message, image))
            return twiml
        twiml.message(message).media(image)
        return twiml
    

request.POST.get('From', '')POST不是用户输入数据的地方吗?从哪里来的?这意味着什么?if '+44' in frm:如果在frm中找到+44?在


Tags: imageformatmessagegetreturnrequestpokeapidescription
1条回答
网友
1楼 · 发布于 2024-06-09 06:57:39

所有问题都是基于非常基本的python概念,我建议您在这里浏览python文档Python Docs

  1. 差异输入请求.POST以及请求.POST.get()

    Ex request.post has following dict {'abc_key': 'abc_value'}
        than request.POST['abc_key'] will give 'abc_value'
        but  request.POST['xyz_key'] will throw error
    
        so we use default value to escape this error
        request.POST.get('xyz_key', "default_value")
        this will not give error if xyz_key is not found
    
  2. 在车身.下部在

    此方法返回字符串的副本,其中所有 字符已被小写。在

    检查此链接lower()

  3. 口袋妖怪['sprites'][0]['resource\u uri'] 这是pokemon中的serching(它有字典值)

    例如pokemon={'sprites':[{'resource\uuri':'res_value'},1,2,3]} 所以pokemon['sprites'][0]['resource_uri']将给出'res_value'

  4. frm=请求.POST.get('From','')和我在第一点说的一样

  5. 如果frm中的“+44”:
    如果字符串'+44'是frm中的子字符串,则返回True 变量(字符串)

相关问题 更多 >