簡體   English   中英

Django對api的HTTP請求

[英]Django HTTP request to api

因此,我一直試圖使它起作用,但與此同時,我不理解其中一些代碼含義。 很抱歉提出這么長時間的問題,但我想了解這些工作原理。

我正在嘗試向另一個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,因此我想更好地了解它們的工作原理和使用方法。

該代碼位於網站的底部。 但是我只是在這里提供代碼,因此對您來說更容易。

網站代碼

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方法/創建函數嗎?

  2. body.lower是什么意思? Cant似乎找不到任何東西。

  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?

所有的問題都是基於非常基本的Python概念,我建議你去通過這里python文檔的Python文檔

  1. 區分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

    此方法返回字符串的副本,其中所有基於大小寫的字符都被小寫。

    檢查此鏈接lower()

  3. pokemon ['sprites'] [0] ['resource_uri']這是在神奇寶貝(具有字典值)中搜索

    防爆。 pokemon = {'sprites':[{'resource_uri':'res_value'},1,2,3]}所以pokemon ['sprites'] [0] ['resource_uri']將給出'res_value'

  4. frm = request.POST.get('From','')與我在第一點說的一樣

  5. 如果在frm中為“ +44”:
    如果字符串'+44'是frm變量中的子字符串,則返回True

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM