0

enter image description hereI'm trying to post a request from postman using form data in body section where I upload a csv file and a JSON string, for which i get a response on postman. I would like to replicate the same in locust to do the load testing where I'm not be able to upload the file as I get the error.

Value Error: Data must not be a string.

Below is the code snippet

def project(self):
    headers = {'content-type': 'application/json', 'X-ACCESS-TOKEN': 'XXXXXXXXXXXXXXXXX'}
    files = [('params_file', ('All_params.csv', open('All_params.csv', 'rb'), 'text/csv'))]

    payload = {"df_params":{"geography":"United Arab Emirates","currency":"United Arab Emirates dirham"}}
    res = self.client.post("/api/project/setup", headers=headers,  data=json.dumps(payload), files=files, catch_response=True)
    response = res.content.decode('utf-8')
    self.response_api = json.loads(response)
    print(self.response_api)
    res.raise_for_status()
Anu Abraham
  • 171
  • 1
  • 2
  • 12
  • Did postman generate this code? If not, can you get postman to generate some code for you (top right of the request designer) and see if the generated code also has this issue? – Caius Jard Jan 11 '19 at 05:49

1 Answers1

0

I guess you can try something like this

from locust import HttpLocust, TaskSet, task


class WebsiteTasks(TaskSet):
    @task
    def setup(self):
        headers = {'content-type': 'application/json', 'X-ACCESS-TOKEN': 'XXXXXXXXXXXXXXXXX'}
        files = [('params_file', ('All_params.csv', open('All_params.csv', 'rb'), 'text/csv'))]
        payload = {"df_params":{"geography":"United Arab Emirates","currency":"United Arab Emirates dirham"}}
        response = self.client.post("/api/project/setup", headers=headers,  data=json.dumps(payload), files=files, catch_response=True)
        response.raise_for_status()


class WebsiteUser(HttpLocust):
    task_set = WebsiteTasks
    min_wait = 5000
    max_wait = 15000

And to run use $ locust -f locustfile.py

Reference : https://locust.io/

Umair Mohammad
  • 4,489
  • 2
  • 20
  • 34
  • I still get the same error. "raise ValueError("Data must not be a string.") ValueError: Data must not be a string." – Anu Abraham Jan 11 '19 at 09:58