python post请求

post请求有4中编码方式

1.application/x-www-form-urlencoded

application/x-www-form-urlencoded是浏览器原生的form表单,提交的数据会按照key1=val1&key2=val2的格式,经过url转码,然后传输

(1)发送post请求

我们除了可以直接编写代码发送post请求,也可以使用postman来构造post请求

使用代码:

import requestsurl = 'https://www.xxxxxx.com/'
# 需要注意的是Content-Length参数如果没有,data表单则不会随着请求被发送给服务端,且使用fiddler抓包的过程中,也无法看到data表单
headers = {'Content-Type': 'application/x-www-form-urlencoded','Content-Length':'<calculated when request is sent>'
}
data = {'a': 1, 'b': 2,'c':'测试'}
result = requests.post(url, headers=headers, data=data)
print(result.content.decode('utf-8'))

使用postman


(2)截获post请求,使用fiddler

(3)接收post请求,返回响应

使用django3的版本,目录结构如下

settings的配置

主路由的配置

from django.contrib import admin
from django.urls import path, re_path, includeurlpatterns = [path('admin/', admin.site.urls),# 将主路由和子路由绑定path('', include('gp_app.urls')),
]

子路由的配置

from django.urls import re_path
from . import viewsurlpatterns = [# name用于给视图命名,可以通过reverse反向解析re_path(r'try_get/', views.get, name='get请求'),re_path(r'try_post', views.post, name='post请求')
]

views.py的配置

from django.shortcuts import render
from django.http import HttpResponse# Create your views here.
def get(request):if request.method == 'get':print(request.GET.getlist('c'))passreturn HttpResponse("ok")def post(request):if request.method == 'POST':print("request.method:", request.method)print("request.POST.getlist('a'):", request.POST.getlist('a'))print("request.POST.getlist('b'):", request.POST.getlist('b'))print("request.POST.getlist('c'):", request.POST.getlist('c'))print("request.POST:", request.POST)a = request.POST.get('a', 0)c = request.POST.get('c', 0)d = str({a: c})  # 需要注意的是,如果要使用HttpResponse来返回响应,参数需要是字符串,d如果不转换成str,返回的结果就是1return HttpResponse(d)

运行django之后,控制台的结果

(4)data表单使用嵌套的数据结构如何处理

情况1,使用json_dumps

postData = {'tid': 1'', 'data': [{'name': 'rqlx', 'value': '0', 'sword': 'attr'},{'name': 'rq1', 'value': '1', 'sword': 'attr'}], 'bindParam': 'true'}
# 注意json.dumps转换成json格式,或许也能写成
# data = json.dumps({'postData':{ 'tid': 1'', 'data': [{'name': 'rqlx', 'value': '0'},{'name': 'rq1', 'value': '1'}], 'bindParam': 'true'}})
# 将json.dumps放在外层
data = {'postData':json.dumps(postData)}
resp = requests.post(url=url,data=data,headers=headers,# cookies=dict_cookie,  # cookie也可以用字典的形式写到headers,类似于’Cookie':'xxxxxxxxxx'timeout=240,
)

情况2:使用url编码

有的时候表单提交时,需要先进行url转码,关键在于后端到底要接收什么类型的数据,如果我们不知道后端能处理的数据,有时就只能靠猜,用不同的方法尝试将表单处理成能被后端解析的格式
from urllib.parse import urlencode
data = {‘a’: 1, ‘b’: 2,‘c’:‘测试’}
data =urlencode(data)
resp = reuquest.post(url=url,headers=headers,data=data)

2.multipart/form-data

multipart/form-data是常用来上传文件的表单

application/json

text/xml

python post请求相关推荐

  1. Python Socket请求网站获取数据

     Python Socket请求网站获取数据 ---阻塞 I/O     ->收快递,快递如果不到,就干不了其他的活 ---非阻塞I/0 ->收快递,不断的去问,有没有送到,有没有送到,. ...

  2. Python网络请求urllib和urllib3详解

    1. 简介 urllib是Python中请求url连接的官方标准库,在Python2中主要为urllib和urllib2,在Python3中整合成了urllib. 而urllib3则是增加了连接池等功 ...

  3. python post请求参数为list_浅谈python3发送post请求参数为空的情况

    post请求的时候如果不带参数,其实作用就跟get请求一样.我们在做接口测试的时候,发现开发就全部使用的post,get的作用就被这样的post空参数请求给替代了. 在Python代码请求,如下: c ...

  4. 生成的头_Python爬虫偷懒神器!快速一键生成Python爬虫请求头

    今天介绍个神奇的网站!堪称爬虫偷懒的神器! 我们在写爬虫,构建网络请求的时候,不可避免地要添加请求头( headers ),以 mdn 学习区为例,我们的请求头是这样的: 一般来说,我们只要添加 us ...

  5. Python requests请求禁止跳转重定向(判断是否为原请求链接)

    python requests请求url,有些网站页面出现:网页不存在(404),301 & 302 跳转的问题,抓取到的页面不是原地址,而是跳转后的页面,通过使用禁止重定向来实现判断. 使用 ...

  6. python 网络请求类库 requests 使用

    python 网络请求类库 requests 使用 requests是 为python封装的强大 REST 操作类库 github https://github.com/kennethreitz/re ...

  7. python request请求参数_使用python将请求的requests headers参数格式化方法

    如下所示: import json # 使用三引号将浏览器复制出来的requests headers参数赋值给一个变量 headers = """ Host: zhan. ...

  8. python如何请求curl_Python爬虫偷懒神器 —— 一键构造请求头!

    今天介绍个神奇的网站!堪称爬虫偷懒的神器! 我们在写爬虫,构建网络请求的时候,不可避免地要添加请求头( headers ),以 mdn 学习区为例,我们的请求头是这样的: Python资源共享群:48 ...

  9. 在python中请求百度easyDL

    在python中请求easyDL 导入所需的库 import requests import json import base64 access_token的获取 url_token = " ...

  10. python requests请求下载百度网盘文件

    python requests请求下载百度网盘文件 注意:这里需要添加网盘账号的cookie值,可以手动登录账号复制cookie(浏览器登录账号后按F12 > 点击Network > 刷新 ...

最新文章

  1. cufflinks基于dataframe数据自定义绘图基于df.iplot功能
  2. Windows Server 2008 和 Windows Vista 结合的功能更加强大
  3. Activemq的连接方式
  4. Exchanging Partitions and Subpartitions with Tables--官方文档
  5. 【产品】腾讯内部的顶级产品课:灵动在细节
  6. tampermonkey参数
  7. Index of Oracle
  8. MyBatis 源码解读-typeHandlerElement()
  9. UDT源代码下载链接
  10. 数据结构与算法基础02:线性表
  11. 对抗神经网络学习(简单的理解)
  12. ubuntu server 20.04安装vnc远程桌面xfce4
  13. 贵就好?中消协买20款扫地机器人,艾罗伯特这款噪音大!
  14. Windows | 用youtube-dl批量下载mp3格式音频
  15. C语言中整型变量四舍五入,怎样将整型变量按四舍五入转换成整数
  16. 简易实现AI虚拟鼠标—手势控制鼠标
  17. 测试不同体重体型软件样子的,hikaku-sitatter身高软件,一键测试自己的体型
  18. 人人商城-数据选择器
  19. 【网站国际化必备】Asp.Net MVC 集成Paypal(贝宝)快速结账 支付接口 ,附源码demo...
  20. 【python学习】列表、元组、字典、集合(详解)

热门文章

  1. 血氧饱和度测量数据显示
  2. 历经8年双11流量洗礼,淘宝开放平台如何攻克技术难关?
  3. 微信公众平台账号资料丢失,如何找回账号密码等登陆信息?
  4. Kubernetes——Kubernetes系统组件与架构
  5. 商品价格竞猜简易版(每天一个python小项目)
  6. JavaScript前端经典面试题之ES6面试题汇总es6
  7. ipv6 介绍,格式定义
  8. 谷歌chrome浏览器被劫持修复
  9. wordcount.java_一步一步编译运行wordcount.java
  10. 最新PowerBI注册PowerBI账号,无需企业邮箱