Locust 请求中的 Python 和参数

问题描述 投票:0回答:1

给定 locust 脚本中的 SequantialTask,我想用 all_pp_ids 列表替换 73808。 如何构造我的发布请求的 data= 参数来代替将列表作为参数?现在它被硬编码为 73808,但我想要 all_pp_ids 中的所有值。

    @task(2)
    def generate_bids_for_all_powerplants(self):
        response = self.client.post(f'https://example.com/bids/generate',
                                    headers={"Authorization": f'Bearer {token}', "Content-Type": 'application/json'},
                                data='{"powerPlantIds":[73808]}', name='/bids/generate')
        print("Response: ", response.request.body)
        print("All ids: ", all_pp_ids)
python locust
1个回答
0
投票

您可以将列表序列化为 json 并将其发送到数据参数中,如下所示:

    all_pp_ids = [73808]
    
    payload_json = json.dumps({"powerPlantIds": all_pp_ids})
    
    response = self.client.post('https://example.com/bids/generate', headers={"Authorization": f'Bearer {token}', "Content-Type": 'application/json'},
 data=payload_json, name='/bids/generate')
© www.soinside.com 2019 - 2024. All rights reserved.