Requests方法 -- post

>>> import requests  导入requests库

>>> help(requests)  #查看requests方法
Help on package requests:

NAME
requests

DESCRIPTION
Requests HTTP Library
~~~~~~~~~~~~~~~~~~~~~

Requests is an HTTP library, written in Python, for human beings. Basic GET
usage:

>>> import requests
>>> r = requests.get(‘https://www.python.org‘)
>>> r.status_code
200
>>> ‘Python is a programming language‘ in r.content
True

... or POST:

>>> payload = dict(key1=‘value1‘, key2=‘value2‘)
>>> r = requests.post(‘https://httpbin.org/post‘, data=payload)
>>> print(r.text)
{
...
"form": {
"key2": "value2",
"key1": "value1"
},
...
}

The other HTTP methods are supported - see `requests.api`. Full documentation
is at <http://python-requests.org>.

:copyright: (c) 2017 by Kenneth Reitz.
:license: Apache 2.0, see LICENSE for more details.

做post实例之字典序列化操作:

>>> import requests
>>> import json
>>> payload = {"yy":"1123","gg":"4145"}
>>> data_json = json.dumps(payload)  # 字典序列化转化用dumps
>>> r = requests.post("https://httpbin.org/post",json=data_json)
>>> print(r.text)
{
"args": {},
"data": "\"{\\\"yy\\\": \\\"1123\\\", \\\"gg\\\": \\\"4145\\\"}\"",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "38",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": "{\"yy\": \"1123\", \"gg\": \"4145\"}",
"origin": "223.104.1.157, 223.104.1.157",
"url": "https://httpbin.org/post"
}

如下为字典:  ---- >也可以写成  >>> payload =dict(yy="1123",gg="4145") 的格式

>>> payload = {"yy":"1123","gg":"4145"}
>>> r = requests.post("https://httpbin.org/post",data=payload)
>>> print(r.text)
{
"args": {},
"data": "",
"files": {},
"form": {
"gg": "4145",
"yy": "1123"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "15",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.21.0"
},
"json": null,
"origin": "223.104.1.157, 223.104.1.157",
"url": "https://httpbin.org/post"
}

如查看某个值:

>>> print(r.json()["form"]["gg"])
4145

原文地址:https://www.cnblogs.com/Teachertao/p/11141230.html

时间: 2024-07-31 13:11:04

Requests方法 -- post的相关文章

Requests方法 --- json模块

1.Json 简介:Json,全名 JavaScript Object Notation,是一种轻量级的数据交换格式,常用于 http 请求中 2.可以用 help(json),查看对应的源码注释内容 Encoding basic Python object hierarchies::>>> import json>>> json.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}])'["foo", {&quo

Requests方法 -- 参数化

import requests#禁用安全请求警告from requests.packages.urllib3.exceptions import InsecureRequestWarningrequests.packages.urllib3.disable_warnings(InsecureRequestWarning)"""1.由于登录时候是多加 2 个 cookie,我们可以先用 get 方法打开登录首页,获取部分 cookie2.再把登录需要的 cookie 添加到 s

Requests方法 -- 参数关联

一.删除草稿箱1.参数这篇https://www.cnblogs.com/Teachertao/p/11144726.html 2.删除刚才保存的草稿 3.用 fiddler 抓包,抓到删除帖子的请求,从抓包结果可以看出,传的 json 参数是 postId 4.这个 postId 哪里来的呢?可以看上个请求 url 地址 5.也就是说保存草稿箱成功之后,重定向一个 url 地址,里面带有 postId 这个参数.那接下来我们提取出来就可以了 二.提取参数1.我们需要的参数 postId 是在保

Requests方法 -- 关联用例执行

1.参照此篇流程 :Requsts方法 -- Blog流程类进行关联 2.用例接口目录如下: 3.用例代码如下: import requestsimport unittestfrom Request.Blog.API_Blog import Blog class Test_Blog(unittest.TestCase): def setUp(self): s = requests.Session() self.blog = Blog(s) # def tearDown(self):cookie

python requests方法post请求json格式处理

方法如下: import requestsimport json data = {    'a': 123,    'b': 456} ## headers中添加上content-type这个参数,指定为json格式headers = {'Content-Type': 'application/json'} ## post的时候,将data字典形式的参数用json包转换成json格式.response = requests.post(url='url', headers=headers, dat

Requests 方法 -- post请求操作实践

1.登录Jenkins抓包 ,小编的Jenkins部署在Tomcat上,把Jenkins.war 包放置到webapps目录. 本次用浏览器自带抓包,按下F12操作,主要看post就可以,登录是向服务器提交表单操作,则为post请求如下: 2.body里面并不是json格式的,而是application/x-www-form-urlencoded,部分请求截图如下: 3.实现登录代母如下: import requests# 先打开登录首页,获取部分 cookieurl1 = "http://lo

Requests方法 -- session方法

import requests#禁用安全请求警告from requests.packages.urllib3.exceptions import InsecureRequestWarningrequests.packages.urllib3.disable_warnings(InsecureRequestWarning) #博客园账号登录请求实例url = "https://account.cnblogs.com/signin?returnurl=https%3A%2F%2Fwww.cnblog

Python-第三方库requests详解

Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求.Requests 的哲学是以 PEP 20 的习语为中心开发的,所以它比 urllib 更加 Pythoner.更重要的一点是它支持 Python3 哦! Beautiful is better than ugly.(美丽优于丑陋) Explicit is better than im

[转载]Python-第三方库requests详解

Requests 是用Python语言编写,基于 urllib,采用 Apache2 Licensed 开源协议的 HTTP 库.它比 urllib 更加方便,可以节约我们大量的工作,完全满足 HTTP 测试需求.Requests 的哲学是以 PEP 20 的习语为中心开发的,所以它比 urllib 更加 Pythoner.更重要的一点是它支持 Python3 哦! Beautiful is better than ugly.(美丽优于丑陋) Explicit is better than im