python脚本之常见参数类型接口调用

Posted 今夜月色很美

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python脚本之常见参数类型接口调用相关的知识,希望对你有一定的参考价值。

get方式url传参

	httpHeader = {}
    # 开始处理
    response = requests.get("http://xxx/test?name=lily", headers=httpHeader, timeout=60)
    # 调用结果解析
    status_code = response.status_code
    if status_code != 200:
        print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
    else:
        print(response.text)

post方式url传参

	httpHeader = {
            "paramType": "JSON(application/json)"
        }
    # 开始处理
    response = requests.post("http://xxx/test?name=zhangsan", headers=httpHeader, timeout=60)
    # 调用结果解析
    status_code = response.status_code
    if status_code != 200:
        print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
    else:
        print(response.text)

post方式application/json传参

	httpHeader = {
            "paramType": "JSON(application/json)"
        }
    body = {"name":"zhangsan"}
    # 开始处理
    response = requests.post("http://xxx/test", json=body, headers=httpHeader, timeout=60)
    # 调用结果解析
    status_code = response.status_code
    if status_code != 200:
        print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
    else:
        print(response.text)

post方式form-urlencoded传参

	httpHeader = {
            "paramType": "x-www-form-urlencoded"
        }
    postdata = {"name":"lisi"}
    body = json.dumps(postdata)
    # 开始处理
    response = requests.post("http://xxx/test", data=body, headers=httpHeader, timeout=60)
    # 调用结果解析
    status_code = response.status_code
    if status_code != 200:
        print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
    else:
        print(response.text)

post方式表单form-data传参

	httpHeader = {}
    # files = {}
    # files["image"] = open("D:\\\\testfile\\\\123.jpg", 'rb')

    files = {"image":open("D:\\\\testfile\\\\123.jpg", 'rb')}
    response = requests.post("http://xxx/test/file", files=files, headers=httpHeader, timeout=60)
    # 调用结果解析
    status_code = response.status_code
    if status_code != 200:
        print("Http请求失败,状态码:" + str(status_code) + ",错误信息:" + response.text)
    else:
        print(response.text)

以上是关于python脚本之常见参数类型接口调用的主要内容,如果未能解决你的问题,请参考以下文章

Python如何调用别人写好的脚本

《Python实例》基础之argparse,提供一个友好的接口说明,老大说我做的真好

《Python实例》基础之argparse,提供一个友好的接口说明,老大说我做的真好

python调用webservice接口之多参数传递

性能工具之JMeter Dubbo 脚本开发

性能工具之JMeter Dubbo 脚本开发