简体   繁体   English

Django对api的HTTP请求

[英]Django HTTP request to api

So i been trying to get this to work but at the same time i do not understand some of these code means. 因此,我一直试图使它起作用,但与此同时,我不理解其中一些代码含义。 I'm sorry for making the question so long but i want to understand how these works. 很抱歉提出这么长时间的问题,但我想了解这些工作原理。

I am trying to make a HTTP request to another API to do POST and GET method using django. 我正在尝试向另一个API发出HTTP请求,以使用Django进行POST和GET方法。 Based on the website code example, which is this url: https://www.twilio.com/blog/2014/11/build-your-own-pokedex-with-django-mms-and-pokeapi.html 基于网站代码示例,该URL如下: https : //www.twilio.com/blog/2014/11/build-your-own-pokedex-with-django-mms-and-pokeapi.html

As i wanted to use HTTP Request on my API to call other API, therefore i wanted to get a better understanding of how these works and how to use it. 由于我想在我的API上使用HTTP请求来调用其他API,因此我想更好地了解它们的工作原理和使用方法。

The code is at the bottom of the website. 该代码位于网站的底部。 But i will just provide the code here so it is easier for you. 但是我只是在这里提供代码,因此对您来说更容易。

website code 网站代码

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

My question is: 我的问题是:

  1. i have read about the request.POST and request.POST.get but i still don't get it. 我已经阅读了有关request.POSTrequest.POST.get但我仍然不明白。 Isn't request.POST = POST method/create function ? 不是request.POST = POST方法/创建函数吗?

  2. what does body.lower mean ? body.lower是什么意思? Cant seems to find anything about it. Cant似乎找不到任何东西。

  3. I am very confuse about this part 我对这部分很困惑

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

is pokemon['sprites'] refers to the sprites field in the api ? pokemon['sprites']是指api中的sprites字段吗?

  1. What does this even means ? 这甚至意味着什么?

      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', '') Isn't POST where user enter data ? request.POST.get('From', '')用户输入数据的位置不是POST吗? Where does 'From' come from? “发件人”来自哪里? And what does this means ? 那是什么意思呢? if '+44' in frm: if +44 is found in frm ? if '+44' in frm:如果在frm中找到+44?

ALL The questions are based on very basic python concepts, I recommend you go through python docs here Python Docs 所有的问题都是基于非常基本的Python概念,我建议你去通过这里python文档的Python文档

  1. Diff in request.POST and request.POST.get() 区分request.POST和request.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. body.lower body.lower

    This method returns a copy of the string in which all case-based characters have been lowercased. 此方法返回字符串的副本,其中所有基于大小写的字符都被小写。

    check this link lower() 检查此链接lower()

  3. pokemon['sprites'][0]['resource_uri'] this is serching in pokemon (which have dictionary values) pokemon ['sprites'] [0] ['resource_uri']这是在神奇宝贝(具有字典值)中搜索

    Ex. 防爆。 pokemon = {'sprites':[{'resource_uri': 'res_value'}, 1, 2, 3 ]} so pokemon['sprites'][0]['resource_uri'] will give 'res_value' pokemon = {'sprites':[{'resource_uri':'res_value'},1,2,3]}所以pokemon ['sprites'] [0] ['resource_uri']将给出'res_value'

  4. frm = request.POST.get('From', '') same as i said in 1st point frm = request.POST.get('From','')与我在第一点说的一样

  5. if '+44' in frm: 如果在frm中为“ +44”:
    this will return True if string '+44' is substring in frm variable(string) 如果字符串'+44'是frm变量中的子字符串,则返回True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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