为 Python LocustIO 格式化 Json
Formatting Json for Python LocustIO
为什么我收到这个 post 调用的错误请求?它与 json 格式有关。如何重新格式化作为参数传递的 json 对象?我正在 运行 使用 LocustIO 和 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)
json.dumps 将 json 对象(列表和字典)作为输入并将其序列化并给出一个字符串作为输出。你用 nameInquiry
喂它,它本身就是一个字符串,因此是错误。
此外post得到一个字典作为输入,所以不需要序列化它。简单的解决方案是将 nameInquiry
设置为 json 对象(注意下面缺少的 """
)并将其直接提供给 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)
否则你可以保留字符串并使用 json.loads:
反序列化它
nameInquiry = json.loads("""
[{
"data": {
"Account Number": "1234567898",...
""")
使用 Locust POST JSON 数据有两种通用方法。我更喜欢将字典传递给 self.client.post
using the json keyword argument 因为我不必担心创建有效的 JSON:
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": ""
},
...
}]
response = self.client.post("/zoneflowsapi/api/Goto/goto/", json=nameInquiry)
另一种方法是手动创建一个有效的 JSON 字符串,或者使用 json.dumps(dict)
创建一个有效的 JSON 字符串,然后将字符串传递给 self.client.post
使用data
参数:
nameInquiryString = """
[{
"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": ""
}
...
}]
"""
response = self.client.post("/zoneflowsapi/api/Goto/goto/", data=nameInquiryString)
如果你使用self.client.put
,你必须使用第二种方法,因为put
方法不支持json
关键字参数。
为什么我收到这个 post 调用的错误请求?它与 json 格式有关。如何重新格式化作为参数传递的 json 对象?我正在 运行 使用 LocustIO 和 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)
json.dumps 将 json 对象(列表和字典)作为输入并将其序列化并给出一个字符串作为输出。你用 nameInquiry
喂它,它本身就是一个字符串,因此是错误。
此外post得到一个字典作为输入,所以不需要序列化它。简单的解决方案是将 nameInquiry
设置为 json 对象(注意下面缺少的 """
)并将其直接提供给 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)
否则你可以保留字符串并使用 json.loads:
反序列化它nameInquiry = json.loads("""
[{
"data": {
"Account Number": "1234567898",...
""")
使用 Locust POST JSON 数据有两种通用方法。我更喜欢将字典传递给 self.client.post
using the json keyword argument 因为我不必担心创建有效的 JSON:
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": ""
},
...
}]
response = self.client.post("/zoneflowsapi/api/Goto/goto/", json=nameInquiry)
另一种方法是手动创建一个有效的 JSON 字符串,或者使用 json.dumps(dict)
创建一个有效的 JSON 字符串,然后将字符串传递给 self.client.post
使用data
参数:
nameInquiryString = """
[{
"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": ""
}
...
}]
"""
response = self.client.post("/zoneflowsapi/api/Goto/goto/", data=nameInquiryString)
如果你使用self.client.put
,你必须使用第二种方法,因为put
方法不支持json
关键字参数。