django API 中接口的互相调用实例
作者:人生海海may 发布时间:2023-11-04 09:36:38
我就废话不多说了,还是直接上代码吧!
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/1
self.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 dumps
from 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 dumps
from 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可能写错了,就要注意一下了,因为通常接口会封装一些参数到请求头底下。
来源:https://blog.csdn.net/CodeMonkeyyyyyyy/article/details/87880473


猜你喜欢
- 很久没有写文章,最近一直在忙于找工作和找房子。哎,现在终于安定下来了,哎,又叹息一下,是因为我把去淘宝面试的机会也推掉了,本来以为要卷铺盖回
- 最近一个项目中使用了ThinkPHP做为开发框架,URL上我们使用了PATHINFO模式,但是Nginx默认是不支持PATHINFO的,需要
- 本文实例讲述了Flask框架各种常见装饰器。分享给大家供大家参考,具体如下:效果类似django的process_request的装饰器@a
- 前提示时间一个博友,建议我提供PHP开发环境的搭建文章。当然我们一般在windows平台下开发的居多,那么,今天我就在Windows平台下搭
- DB2 存储过程:基础知识您在客户端工作站上对远程服务器和位于该服务器上的数据库进行分类的任何时候,都存在一个简单的 DB2 客户端/服务器
- 本文实例为大家分享了python实现画圆功能的具体代码,供大家参考,具体内容如下import numpy as np import matp
- 可以让我们将数据用表格的方式展示出来安装方式pip install PrettyTable测试是否安装成功使用方法与对比增加一条数据先简单的
- 相信大家在微信上一定被上面的这段话刷过屏,群发消息应该算是微信上流传最广的找到删除好友的方法了。但群发消息不仅仅会把通讯录里面所有的好友骚扰
- 建造者模式的适用范围:想要创建一个由多个部分组成的对象,而且它的构成需要一步接一步的完成。只有当各个部分都完成了,这个对象才完整。建造者模式
- 计算字符长度的js函数function LEN(str){ var i,sum=0; for(i=0;i<str.length;i++
- 直接转换就行了,key为DataFrame的column;import pandas as pddata = pd.read_csv(
- K近邻法是有监督学习方法,原理很简单,假设我们有一堆分好类的样本数据,分好类表示每个样本都一个对应的已知类标签,当来一个测试样本要我们判断它
- 在实际数据分析过程中,我们分析用Python来处理数据(海量的数据),我们都是把这个数据转换为Python的对象的,比如最为常见的字典。比如
- 前言发现自己学习python 的各种库老是容易忘记,所有想利用这个平台,记录和分享一下学习时候的知识点,以后也能及时的复习,最近学习pand
- 前言业务需求中需要连接两个数据库处理数据,需要用动态数据源。通过了解mybatis的框架,计划 使用分包的方式进行数据源的区分。原理前提:我
- 一、AIML是什么AIML全名为Artificial Intelligence Markup Language(人工智能标记语言),是一种创
- 第一种 #This File was made using the WinMySQLAdmin 1.4 Tool #2004-2-23 16
- 简单定义图轴:import numpy as npimport matplotlib.pyplot as plt创建一个简单的matplot
- SQL server2000数据库应用非常广泛,一旦出现安全问题,造成的损失往往难以估量!应提前预防,防患于未然。这里,我们主要谈论有关SQ
- 这是一篇关于使用JScript RuntimeObject(MSDN)调试的文章。虽然这些例子中的大多数在其他浏览器中不能运行,但在IE 5