zoukankan      html  css  js  c++  java
  • pyhton接口自动化测试-requests.post()

    一、方法定义

    二、post方法简单使用

      1、带数据的post

      2、带header的post

      3、带json的post

      4、带参数的post

      5、普通文件上传

      6、定制化文件上传

      7、多文件上传

    一、方法定义:

    1、到官方文档去了下requests.post()方法的定义,如下:

    2、源码:

    3、常用返回信息:

    二、post方法简单使用:

     1、带数据的post:

    # -*- coding:utf-8 -*-
    import requests
    import json
    
    host = "http://httpbin.org/"
    endpoint = "post"
    url = ''.join([host,endpoint])
    data = {'key1':'value1','key2':'value2'}
    
    r = requests.post(url,data=data)
    #response = r.json()
    print ((r.text))

    输出:

    {
      "args": {}, 
      "data": "", 
      "files": {}, 
      "form": {
        "key1": "value1", 
        "key2": "value2"
      }, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Connection": "close", 
        "Content-Length": "23", 
        "Content-Type": "application/x-www-form-urlencoded", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.18.1"
      }, 
      "json": null, 
      "origin": "183.14.133.88", 
      "url": "http://httpbin.org/post"
    }
    View Code

    2、带header的post:

    # -*- coding:utf-8 -*-
    import requests
    import json
    
    host = "http://httpbin.org/"
    endpoint = "post"
    
    url = ''.join([host,endpoint])
    headers = {"User-Agent":"test request headers"}
    
    # r = requests.post(url)
    r = requests.post(url,headers=headers)
    #response = r.json()

    输出:

    {
      "args": {}, 
      "data": "", 
      "files": {}, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Connection": "close", 
        "Content-Length": "0", 
        "Host": "httpbin.org", 
        "User-Agent": "test request headers"
      }, 
      "json": null, 
      "origin": "183.14.133.88", 
      "url": "http://httpbin.org/post"
    View Code

    3、带json的post:

    import requests
    import json
    
    host = "http://httpbin.org/"
    endpoint = "post"
    
    url = ''.join([host,endpoint])
    data = {
        "sites": [
                    { "name":"test" , "url":"www.test.com" },
                    { "name":"google" , "url":"www.google.com" },
                    { "name":"weibo" , "url":"www.weibo.com" }
        ]
    }
    
    r = requests.post(url,json=data)
    # r = requests.post(url,data=json.dumps(data))
    response = r.json()

    输出:

    {
      "args": {}, 
      "data": "{"sites": [{"name": "test", "url": "www.test.com"}, {"name": "google", "url": "www.google.com"}, {"name": "weibo", "url": "www.weibo.com"}]}", 
      "files": {}, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Content-Length": "140", 
        "Content-Type": "application/json", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.21.0"
      }, 
      "json": {
        "sites": [
          {
            "name": "test", 
            "url": "www.test.com"
          }, 
          {
            "name": "google", 
            "url": "www.google.com"
          }, 
          {
            "name": "weibo", 
            "url": "www.weibo.com"
          }
        ]
      }, 
      "origin": "113.65.130.163, 113.65.130.163", 
      "url": "https://httpbin.org/post"
    }
    View Code

    4、带参数的post:

    # -*- coding:utf-8 -*-
    import requests
    import json
    
    host = "http://httpbin.org/"
    endpoint = "post"
    
    url = ''.join([host,endpoint])
    params = {'key1':'params1','key2':'params2'}
    
    # r = requests.post(url)
    r = requests.post(url,params=params)
    #response = r.json()
    print (r.text)

    输出:

    {
      "args": {
        "key1": "params1", 
        "key2": "params2"
      }, 
      "data": "", 
      "files": {}, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Connection": "close", 
        "Content-Length": "0", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.18.1"
      }, 
      "json": null, 
      "origin": "183.14.133.88", 
      "url": "http://httpbin.org/post?key2=params2&key1=params1"
    }
    View Code

    5、普通文件上传:

    # -*- coding:utf-8 -*-
    import requests
    import json
    
    host = "http://httpbin.org/"
    endpoint = "post"
    
    url = ''.join([host,endpoint])
    #普通上传
    files = {
                'file':open('test.txt','rb')
            }
    
    r = requests.post(url,files=files)
    print (r.text)

    输出:

    {
      "args": {}, 
      "data": "", 
      "files": {
        "file": "hello world!
    "
      }, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Connection": "close", 
        "Content-Length": "157", 
        "Content-Type": "multipart/form-data; boundary=392865f79bf6431f8a53c9d56c62571e", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.18.1"
      }, 
      "json": null, 
      "origin": "183.14.133.88", 
      "url": "http://httpbin.org/post"
    }
    View Code

    6、定制化文件上传:

    # -*- coding:utf-8 -*-
    import requests
    import json
    
    host = "http://httpbin.org/"
    endpoint = "post"
    
    url = ''.join([host,endpoint])
    #自定义文件名,文件类型、请求头
    files = {
            'file':('test.png',open('test.png','rb'),'image/png')
    }
    
    r = requests.post(url,files=files)
    print (r.text)heman793

    7、多文件上传:

    # -*- coding:utf-8 -*-
    import requests
    import json
    
    host = "http://httpbin.org/"
    endpoint = "post"
    
    url = ''.join([host,endpoint])
    #多文件上传
    files = [
        ('file1',('test.txt',open('test.txt', 'rb'))),
        ('file2', ('test.png', open('test.png', 'rb')))
        ]
    
    r = requests.post(url,files=files)
    print (r.text)

    8、流式上传:

    # -*- coding:utf-8 -*-
    import requests
    import json
    
    host = "http://httpbin.org/"
    endpoint = "post"
    
    url = ''.join([host,endpoint])
    
    #流式上传
    with open( 'test.txt' ) as f:
        r = requests.post(url,data = f)
    
    print (r.text)

    输出:

    {
      "args": {}, 
      "data": "hello world!
    ", 
      "files": {}, 
      "form": {}, 
      "headers": {
        "Accept": "*/*", 
        "Accept-Encoding": "gzip, deflate", 
        "Connection": "close", 
        "Content-Length": "13", 
        "Host": "httpbin.org", 
        "User-Agent": "python-requests/2.18.1"
      }, 
      "json": null, 
      "origin": "183.14.133.88", 
      "url": "http://httpbin.org/post"
    }
  • 相关阅读:
    centos7.9安装Oracle 19c数据库(rpm方式)
    tomcat配置404/500自定义错误页面
    centos7.7部署多个tomcat服务器
    Linux配置Tomcat实现双向SSL认证(使用keytool工具生成证书)
    C#方法的多值返回方式
    Unity 判断2D/3D坐标系下的一个点是否在指定的摄像机视野范围内
    Unity项目的APK在雷电模拟器上弹出 "XXX停止运行"或者“XXX屡次停止运行”
    Python调用Unity工程下CSharp代码中的静态方法
    C#如何屏蔽程序的多次启动
    Something about My Blog
  • 原文地址:https://www.cnblogs.com/lanyinhao/p/11122360.html
Copyright © 2011-2022 走看看