Skip to content
Advertisement

Formatting Json for Python LocustIO

Why am I getting bad request for this post calls? It has to do with json formatting. How do I reformat the json object passed as param? I’m running a load test using LocustIO and Python.

from locust import HttpLocust, TaskSet, task
from slumber import API 
import json, requests

nameInquiry = """
[{
  "data": {
    "Account Number": "1234567898",
    "Bank Code": "EBN",
    "AppId": "com.appzonegroup.zone",
    "deviceId": "a4a7427032c286e3",
    "Sender Phone Number": "+2348094399450",
    "Sender Conversation ID": "161503479186618e8726fc4-70c0-4768-a2ea-5217c3a3c26d",
    "FileId": ""
  },
  "instruction": {
    "FunctionId": "",
    "FlowId": "813dac4f-7e44-4873-b45f-f6f3b5dbe436",
    "InstitutionCode": "",
    "TimeOutSeconds": 30
  }
}]
"""
myheaders = {'Content-Type': 'application/json', 'Accept': 'application/json'}


class NameInquiries(TaskSet):
  @task(1)
  def send(self):
    response = self.client.post("/zoneflowsapi/api/Goto/goto/", data=json.dumps(nameInquiry), headers= myheaders )

    print("Response status code:", response.status_code)
    print("Response content:", response.text)

Advertisement

Answer

json.dumps takes as input json object (lists & dictionaries) and serializes it giving a string as output. You are feeding it with nameInquiry, which is already a string by itself, thus the error.

Furthermore post gets a dictionary as input, so there is no need to serialize it. The simple solution is to set nameInquiry as a json object (notice the missing """ below) and feed it directly to post.

nameInquiry = [{
    "data": {
    "Account Number": "1234567898",
    "Bank Code": "EBN",
    "AppId": "com.appzonegroup.zone",
    ...
}]

...

response = self.client.post("/zoneflowsapi/api/Goto/goto/", data=nameInquiry, headers=myheaders)

Otherwise you can keep the string and deserialize it using json.loads:

nameInquiry = json.loads("""
[{
  "data": {
  "Account Number": "1234567898",...
""")
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement