色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

django API 中接口的互相調用實例

瀏覽:122日期: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)

獲取當前請求的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).

獲取請求頭內容的用META

示例:

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

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

補充知識:django 使用requests請求相關接口

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

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的字符串數據。

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

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這個參數,必須要用dumps(data)轉換一下,不然會報錯,response狀態碼為400,bad request error 400 while using python requests.post function。

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

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的狀態碼報415錯誤的話,即HTTP請求415錯誤 ? 不支持的媒體類型(Unsupported media type),這就是content-Type可能寫錯了,就要注意一下了,因為通常接口會封裝一些參數到請求頭底下。

以上這篇django API 中接口的互相調用實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Django
相關文章:
主站蜘蛛池模板: 九九精品免费观看在线 | 欧美69 | 在线日韩中文字幕 | 国产精品久久久久久网站 | 国产日韩精品视频一区二区三区 | 亚洲最新视频在线观看 | 久久精品资源 | 免费的三级网站 | 欧美一级特毛片 | 免费国产成人高清在线看软件 | 亚洲国产日韩a在线亚洲 | 91在线 | 亚洲| 在线观看a网站 | 亚洲免费不卡 | 亚洲欧美极品 | 欧美日韩国产亚洲一区二区 | 国内免费视频成人精品 | 黄在线看| 国产免费人成在线看视频 | 久久99精品视香蕉蕉 | 国产视频高清在线观看 | 男女牲高爱潮免费视频男女 | 国产高中生粉嫩无套第一次 | 成人一级黄色片 | 日韩精品另类天天更新影院 | 三级国产在线 | 91日本在线精品高清观看 | 国产成人精品天堂 | 男女男精品视频网站在线观看 | 欧美一区二区在线观看视频 | 欧美操操操 | 色综合久久88色综合天天 | 久久影院国产 | 免费午夜扒丝袜www在线看 | 一级特黄aaa大片在线观看 | 亚洲精品国产三级在线观看 | 久久国产成人午夜aⅴ影院 久久国产成人亚洲精品影院老金 | 99久久99久久精品免费看子伦 | 特级毛片在线播放 | 国产亚洲福利一区二区免费看 | 草免费视频 |