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.
JavaScript
x
34
34
1
from locust import HttpLocust, TaskSet, task
2
from slumber import API
3
import json, requests
4
5
nameInquiry = """
6
[{
7
"data": {
8
"Account Number": "1234567898",
9
"Bank Code": "EBN",
10
"AppId": "com.appzonegroup.zone",
11
"deviceId": "a4a7427032c286e3",
12
"Sender Phone Number": "+2348094399450",
13
"Sender Conversation ID": "161503479186618e8726fc4-70c0-4768-a2ea-5217c3a3c26d",
14
"FileId": ""
15
},
16
"instruction": {
17
"FunctionId": "",
18
"FlowId": "813dac4f-7e44-4873-b45f-f6f3b5dbe436",
19
"InstitutionCode": "",
20
"TimeOutSeconds": 30
21
}
22
}]
23
"""
24
myheaders = {'Content-Type': 'application/json', 'Accept': 'application/json'}
25
26
27
class NameInquiries(TaskSet):
28
@task(1)
29
def send(self):
30
response = self.client.post("/zoneflowsapi/api/Goto/goto/", data=json.dumps(nameInquiry), headers= myheaders )
31
32
print("Response status code:", response.status_code)
33
print("Response content:", response.text)
34
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.
JavaScript
1
12
12
1
nameInquiry = [{
2
"data": {
3
"Account Number": "1234567898",
4
"Bank Code": "EBN",
5
"AppId": "com.appzonegroup.zone",
6
7
}]
8
9
10
11
response = self.client.post("/zoneflowsapi/api/Goto/goto/", data=nameInquiry, headers=myheaders)
12
Otherwise you can keep the string and deserialize it using json.loads:
JavaScript
1
6
1
nameInquiry = json.loads("""
2
[{
3
"data": {
4
"Account Number": "1234567898",
5
""")
6