我其实是个新手 2023-12-21 20:09 采纳率: 20%
浏览 3

python使用locust的client发布送文件请求

原数据格式:

img

JSONArray array = object.getJSONArray("files");
        array.stream()
                .filter(e -> array != null)
                .filter(e -> !array.isEmpty())
                .forEach(obj -> {
                    JSONObject json = (JSONObject) obj;
                    String[] names = StringUtils.split(json.get("name").toString(), "/");
                    // 取出content字符转成流循环构建进入 multiPart中
                    requestSpecification.multiPart("files", names[names.length - 1], new ByteArrayInputStream(ReadJsonUtils.objectToJson(json.get("content")).getBytes(Charset.defaultCharset())));
                });
        return requestSpecification
                .post(url
                .then().statusCode(200).extract().response();

我现在想使用locuts中的client进行 http请求 我应该怎么传入以上的数据 以下为python代码


"""
        模拟发送数据
        """
        form_data = {
            "roleList": [],
            "resourceList": [],
            "resourceDataList": [],
            "roleResourceMappingList": [],
            "reportIdList": [],
            "appId": app_id,
            "env": "",
            "logicPageResourceDtoList": {},
            "logicAuthFlag": False,
            "callLogicValidations": {},
            "callbackLogicsName": [],
            "frontends": [],
            "replicas": 1,
        }
        with open('resources/deployment.json', 'r', encoding='utf-8') as deployment_file:
            deployment_str = json.load(deployment_file)
        shared_keys = set(form_data.keys()) & set(deployment_str.keys())
        form_data.update({key: deployment_str[key] for key in shared_keys})

        files = deployment_str["files"]
        file_list = []
        for file_data in files:
            aa = {"files", io.BytesIO(file_data.get("content").encode('utf-8'))}
            file_list.append(aa)

        headers = {
            "Cookie": self.parent.cookie,
            "Content-Type": "multipart/form-data",
        }

        response = self.client.post(url="/api/v1/app/deployments", data=form_data, files=file_list, headers=headers)
        if response.status_code == 200:
            response_body = response.json()
            print(f"response_body: {response_body}")
            code = response_body.get('code')
            if code == 200:
                print(f"请求成功")
            else:
                request_id = response.headers['requestId']
                print(f"请求失败: {request_id}")
        else:
            print(f"接口调用失败: {response.status_code}")
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2023-12-26 00:53
    关注

    【相关推荐】




    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

问题事件

  • 修改了问题 12月21日
  • 创建了问题 12月21日

悬赏问题

  • ¥30 为什么会失败呢,该如何调整
  • ¥50 如何在不能联网影子模式下的电脑解决usb锁
  • ¥20 服务器redhat5.8网络问题
  • ¥15 如何利用c++ MFC绘制复杂网络多层图
  • ¥20 要做柴油机燃烧室优化 需要保持压缩比不变 请问怎么用AVL fire ESE软件里面的 compensation volume 来使用补偿体积来保持压缩比不变
  • ¥15 python螺旋图像
  • ¥15 算能的sail库的运用
  • ¥15 'Content-Type': 'application/x-www-form-urlencoded' 请教 这种post请求参数,该如何填写??重点是下面那个冒号啊
  • ¥15 找代写python里的jango设计在线书店
  • ¥15 请教如何关于Msg文件解析