首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Django对api的HTTP请求

Django对api的HTTP请求
EN

Stack Overflow用户
提问于 2018-01-18 09:44:20
回答 1查看 1.6K关注 0票数 0

因此,我一直试图使这个工作,但同时,我不明白其中的一些代码意味着。我很抱歉提了这么长时间的问题,但我想了解一下这些问题是如何工作的。

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

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

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

网站代码

代码语言:javascript
运行
复制
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 =口袋妖怪‘精灵’‘资源_uri’description_uri =口袋妖怪‘描述’资源_uri‘雪碧= query_pokeapi(sprite_uri) description = query_pokeapi(description_uri)

pokemon['sprites']是指api中的sprites字段吗?

  1. 这到底是什么意思? frm = request.POST.get('From','') if '+44‘在frm: twiml.message('{0} {1}’.format(消息,图像))返回twiml twiml.message(消息).media(图像)返回twiml

request.POST.get('From', '')不发布用户输入数据的位置吗?“从哪里来”?这意味着什么?如果在frm中发现了if '+44' in frm: +44?

EN

回答 1

Stack Overflow用户

发布于 2018-01-18 10:03:51

所有问题都是基于非常基本的python概念,我建议您在这里阅读python ( Python文档 )。

  1. request.POST和request.POST.get()中的Diff Ex request.post有以下命令{'abc_key':'abc_value'}而不是请求。dict‘abc_key’将给出'abc_value‘,但是request.POST' xyz_key’将抛出错误,因此我们使用默认值来转义此错误request.POST.get('xyz_key',"default_value"),如果找不到xyz_key,则不会产生错误。
  2. body.lower 此方法返回字符串的副本,其中所有基于大小写的字符都已被小写。 检查这个链接较低()
  3. 口袋妖怪‘精灵’‘资源_uri’这是在口袋妖怪(它有字典值)中的服务器。 例如。口袋妖怪={‘精灵’:{‘res_value_uri’},1,2,3}因此口袋妖怪的“精灵”资源_uri将给出'res_value‘。
  4. frm = request.POST.get('From','')与我在第一点中所说的相同
  5. 如果frm中的“+44”: 如果字符串'+44‘是frm变量( string )中的子字符串,则返回True。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48318139

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档