pythonlocust

Python and parameter in Locust request


Given a SequantialTask in a locust script I want to replace 73808 with the list all_pp_ids. How can I construct the data= parameter of my post request to instead take in the list as parameter? Now it is hardcoded to 73808 but I want alle values in all_pp_ids instead.

    @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)

Solution

  • You can serialize your list to json and send it in data parameter like this:

        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')