久久福利_99r_国产日韩在线视频_直接看av的网站_中文欧美日韩_久久一

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

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

瀏覽:151日期: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
相關文章:
主站蜘蛛池模板: 日韩另类| 伊人www| www.中文字幕.com | 国产偷国产偷精品高清尤物 | 日韩一区二区久久 | 精品一区二区不卡 | 亚洲成人av电影 | 97国产超碰 | 久久亚洲天堂 | 91夜夜| 久久精品国产精品青草 | 亚洲午夜电影在线 | 国产69精品久久久久观看黑料 | 久久精品一区二区三区四区 | 在线免费观看av片 | 欧美性www| 极品一区| 亚洲黄色国产 | 亚洲一区二区三区高清 | 中文字幕在线观看 | 精品国产伦一区二区三区观看说明 | 亚洲综合99| 午夜久久久久 | 另类久久 | 国产在线乱 | 四色成人av永久网址 | 国产精品一区二 | 夜夜骑天天干 | 久草青青 | 久久国产精品99久久久久久牛牛 | 99国产精品久久久久久久 | 午夜影院免费看 | 精品九九| 久久99国产精品久久99大师 | 国产偷自视频区视频 | 国产欧美日韩精品一区 | 欧美精品一区二区三区蜜桃视频 | 日本一区二区电影 | 日韩午夜一级片 | 一区二区三区亚洲精品国 | 日韩在线国产 |