zoukankan      html  css  js  c++  java
  • python上传文件接口

    文件的参数组装:

        ('文件名',"open打开的文件(rb模式打开)",'文件的类型说明')

     

    关于不同的请求参数类型,使用requests的处理:

    1、文件上传(Content-Type: multipart/form-data;),使用files传递

        requests.post(url='xxx',files=文件参数)

        

    2、表单参数(Content-Type: application/x-www-form-urlencoded;),使用data传递

        requests.post(url='xxx',data=表单参数)

     

    3json参数(Content-Type: application/json),使用json传递

        requests.post(url='xxx',json=json参数)

     

    4、查询字符串(拼接在url地址后面的),params传递

        requests.post(url='xxx',params=查询字符串参数)

     

    """

    import requests
    
     
    
    # 第一步:准备请求接口所需的数据
    
    # 1、接口地址
    
    url = "http://127.0.0.1:5000/upload"
    
    
    # 2、请求参数
    
    params = {
    
        "nickname": "木森",
    
        "age": 18,
    
        "sex": ""
    
    }
    
    # 文件的参数组装
    
    file = {
    
        "pic": ("kejian.ppt", open(r"C:课件课件模板.ppt", "rb"), "text/txt")
    
    }
    
     
    
    # 使用多个参数上传多个文件的时候,参数的组装形式
    
    # file = {
    
    #     "pic": ("kejian.ppt", open(r"C:课件课件模板.ppt", "rb"), "text/txt"),
    
    #     "pic2": ("lmb.png", open("lmb.png", "rb"), "text/txt")
    
    # }
    
     
    
    # 同一个参数上传多个文件的时候,参数的组装形式
    
    # files = [
    
    #     ("pic", ("kejian.ppt", open(r"C:课件课件模板.ppt", "rb"), "text/txt")),
    
    #     ("pic", ("lmb.png", open("lmb.png", "rb"), "text/txt")),
    
    # ]
    
    # 第二步发送请求 params  data  json,files
    
    # 文件上传的接口,上传的文件需要使用files来进行传递
    
    res = requests.post(url=url, data=params, files=file)
    
    # 第三步:获取返回结果
    
    print(res.json())
  • 相关阅读:
    fiddler的使用
    redis pipeline
    redis hash map
    redis队列的实现
    PHP-redis中文文档-命令
    websocket
    c++之socket,阻塞模式
    Django上传文件和修改date格式
    通过字符串导入模块
    'CSRFCheck' object has no attribute 'process_request' 报错
  • 原文地址:https://www.cnblogs.com/crystal1126/p/13591049.html
Copyright © 2011-2022 走看看