成人视屏在线观看-国产99精品-国产精品1区2区-欧美一级在线观看-国产一区二区日韩-色九九九

您的位置:首頁技術(shù)文章
文章詳情頁

django API 中接口的互相調(diào)用實(shí)例

瀏覽:164日期:2024-10-13 10:14:56

我就廢話不多說了,還是直接上代碼吧!

url = 'http://%s:%s/api-token-auth/' % (ip, port) query_args = { 'username': username, 'password': password } resp = requests.post(url=url, data=query_args) token = json.loads(resp.text)['token'] headers = {'Authorization': 'JWT' + ' ' + token} # 拿到token,拼成headers post_url = 'http://%s:%s/message/message-level-two/'% (ip, port) data = { 'app': app, 'url': url, 'message_id': message_id, 'head': head, 'title': title, 'userprofile_id_list': userprofile_id_list } headers = self.headers requests.post(url=post_url, data=data, headers=headers)

獲取當(dāng)前請求的ip和端口

host_ip, host_port = self.request.META.get('HTTP_HOST').split(’:’)[0], self.request.META.get('HTTP_HOST').split(’:’)[1]

常見的請求頭如下:

CONTENT_LENGTH ? The length of the request body (as a string).CONTENT_TYPE ? The MIME type of the request body.HTTP_ACCEPT ? Acceptable content types for the response.HTTP_ACCEPT_ENCODING ? Acceptable encodings for the response.HTTP_ACCEPT_LANGUAGE ? Acceptable languages for the response.HTTP_HOST ? The HTTP Host header sent by the client.HTTP_REFERER ? The referring page, if any.HTTP_USER_AGENT ? The client’s user-agent string.QUERY_STRING ? The query string, as a single (unparsed) string.REMOTE_ADDR ? The IP address of the client.REMOTE_HOST ? The hostname of the client.REMOTE_USER ? The user authenticated by the Web server, if any.REQUEST_METHOD ? A string such as 'GET' or 'POST'.SERVER_NAME ? The hostname of the server.SERVER_PORT ? The port of the server (as a string).

獲取請求頭內(nèi)容的用META

示例:

def index(request): ip = request.META.get('REMOTE_ADDR') return HttpResponse('你的ip地址是%s'%ip)

http://10.254.30.27/1self.kwargs[‘pk’] # 可以拿到后邊的 1

補(bǔ)充知識:django 使用requests請求相關(guān)接口

1、如果是get請求接口,并且需要帶相關(guān)參數(shù)的話,可以借鑒下面的代碼:

import requests from django.http import JsonResponse def get_info(request): url = ’http://www.baidu.com’ params = {’id’: 1, ’user’: ’lin’} response = requests.get(url=url, params=params) return JsonResponse(response.text, safe=False)

這樣將會返回一串json的字符串?dāng)?shù)據(jù)。

2、如果是post請求接口,并且需要帶相關(guān)參數(shù)的話,可以借鑒下面的代碼:

import requests from json import dumpsfrom django.http import JsonResponse def get_info(request): url = ’http://www.baidu.com’ data = {’id’: 1, ’user’: ’lin’} response = requests.post(url=url, data=dumps(data)) return JsonResponse(response.text, safe=False)

注:

(1)、其中必須注意的為data這個(gè)參數(shù),必須要用dumps(data)轉(zhuǎn)換一下,不然會報(bào)錯,response狀態(tài)碼為400,bad request error 400 while using python requests.post function。

(2)、如果需要在post請求底下加相關(guān)請求頭的話,可以借鑒下面的代碼:

import requests from json import dumpsfrom django.http import JsonResponse def get_info(request): url = ’http://www.baidu.com’ data = {’id’: 1, ’user’: ’lin’} headers = {’content-Type’: ’application/json’, ’Accept’: ’*/*’} response = requests.post(url=url, data=dumps(data), headers=headers) return JsonResponse(response.text, safe=False)

這里如果response的狀態(tài)碼報(bào)415錯誤的話,即HTTP請求415錯誤 ? 不支持的媒體類型(Unsupported media type),這就是content-Type可能寫錯了,就要注意一下了,因?yàn)橥ǔ=涌跁庋b一些參數(shù)到請求頭底下。

以上這篇django API 中接口的互相調(diào)用實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 国产亚洲欧美日韩在线看片 | 亚洲成人免费在线观看 | 亚洲日本va午夜中文字幕 | 欧美激情久久久久久久大片 | 成人国产精品一级毛片了 | 欧美视频精品在线观看 | 韩国一级淫片视频免费播放 | 欧美激情成人网 | 精品国产一区二区三区www | 亚洲精品中文字幕久久久久久 | 成人女人a毛片在线看 | 夜色成人性y | 日本高清无吗免费播放 | 亚洲一区三区 | 日韩美女一级视频 | 日本欧美一区二区三区不卡视频 | 一级风流片a级国产 | 国产成人精品综合久久久 | 午夜精品久视频在线观看 | 99久久综合 | 色综合天天综合网看在线影院 | 久久99爱视频| 美女黄色毛片免费看 | 欧美一级棒 | 黄色片成年人 | 加勒比色综合久久久久久久久 | 国产精选一区二区 | 日本免费一区二区三区看片 | 欧美大片一区 | 欧美视频精品一区二区三区 | 精品网址| 国产精品黄在线观看免费软件 | 国产精品久久久久一区二区 | 久久久一本精品99久久精品66 | 日本美女福利视频 | 免费欧美一级片 | 精品免费久久久久国产一区 | 91chinesevideo在线观看 | 欧美综合成人 | 韩国精品一区视频在线播放 | 亚洲第一页在线视频 |