簡體   English   中英

如何在 python 中寫入正確的 json

[英]how to write correct json in python

有人可以解釋我為什么會收到 422 錯誤嗎?

我有來自 Postman 的 JSON,現在我正試圖在 Locust 中發送這個 JSON。 但是我一直收到 422 錯誤。

這是來自 Postman 的 JSON:

data = {"startDate": "2020-10-01T00:00:00.000Z",
                "endDate": "2020-10-11T00:00:00.000Z",
                "id": 117,
                "title": "Job Postmanowy Contractora 156",
                "desc": "Backendowe opowiastki",
                "active": true,
                "isDraft": false,
                "isPaused": false,
                "isBlocked": false,
                "locationPayload": "{\"locationType\":\"locationPlace\",\"locationName\":\"Wałbrzych, Dolnośląskie, Poland\",\"locationPlace\":\"place.8245570224125220\",\"locationRegion\":\"region.11228101935550230\",\"locationCountry\":\"country.5811537771766020\"}",
                "locationName": "Minneapolis, MN, USA",
                "workingHours": "No matter",
                "employmentType": "other",
                "assignmentType": "indi",
                "gender": "both",
                "minWorkers": 3,
                "maxWorkers": 5,
                "minHeight": 10,
                "minWeight": 10,
                "maxHeight": 180,
                "maxWeight": 80,
                "minAge": 18,
                "maxAge": 25,
                "minWage": 20,
                "maxWage": 20,
                "wageFrequency": "per hour",
                "paymentType": "credit card",
                "updatedBy": null,
                "createdAt": "2020-07-10T12:34:18.000Z",
                "updatedAt": "2020-07-10T12:34:20.000Z",
                "deletedAt": null,
                "userId": 100,
                "canApply": false,
                "applied": false,
                "applicationStatus": null,
                "isMine": true,
                "status": "future",
                "isFavourite": false,
                "applications": {"applied": 0, "accepted": 0, "declined": 0},
                "isEditable": true,
                "location": {},
                "user": {"id": 100,
                         "fullName": "Con SzamsungS9 1",
                         "companyName": "", "rating": null,
                         "avatar": "files/3859ca8b6afde6838cf3b6fce356dbaf67359d252f5670d87d6a61e3c7149377image-e4212eac-e0b8-40e7-b009-d3907bf51a5a.jpg"},
                "languages": ["German", "English", "Spanish", "Hindi", "Italian", "Polish"],
                "highlights": ["high-voltage", "heavy materials"]}

使用小寫 boolean 我有創建參數的提示,當我更改為大寫時我有 422。

這就是我想發布我的 JSON 的方式

self.client.post('/v1/jobs/drafts/', headers=self.headers, data=json.dumps(data))

這應該有效:

首先使用 python 語法將您的 json 創建為字典:

data = {"startDate": "2020-10-01T00:00:00.000Z",
                "endDate": "2020-10-11T00:00:00.000Z",
                "id": 117,
                "title": "Job Postmanowy Contractora 156",
                "desc": "Backendowe opowiastki",
                "active": True,
                "isDraft": False,
                "isPaused": False,
                "isBlocked": False,
                "locationPayload": "{\"locationType\":\"locationPlace\",\"locationName\":\"Wałbrzych, Dolnośląskie, Poland\",\"locationPlace\":\"place.8245570224125220\",\"locationRegion\":\"region.11228101935550230\",\"locationCountry\":\"country.5811537771766020\"}",
                "locationName": "Minneapolis, MN, USA",
                "workingHours": "No matter",
                "employmentType": "other",
                "assignmentType": "indi",
                "gender": "both",
                "minWorkers": 3,
                "maxWorkers": 5,
                "minHeight": 10,
                "minWeight": 10,
                "maxHeight": 180,
                "maxWeight": 80,
                "minAge": 18,
                "maxAge": 25,
                "minWage": 20,
                "maxWage": 20,
                "wageFrequency": "per hour",
                "paymentType": "credit card",
                "updatedBy": None,
                "createdAt": "2020-07-10T12:34:18.000Z",
                "updatedAt": "2020-07-10T12:34:20.000Z",
                "deletedAt": None,
                "userId": 100,
                "canApply": False,
                "applied": False,
                "applicationStatus": None,
                "isMine": True,
                "status": "future",
                "isFavourite": False,
                "applications": {"applied": 0, "accepted": 0, "declined": 0},
                "isEditable": True,
                "location": {},
                "user": {"id": 100,
                         "fullName": "Con SzamsungS9 1",
                         "companyName": "", "rating": None,
                         "avatar": "files/3859ca8b6afde6838cf3b6fce356dbaf67359d252f5670d87d6a61e3c7149377image-e4212eac-e0b8-40e7-b009-d3907bf51a5a.jpg"},
                "languages": ["German", "English", "Spanish", "Hindi", "Italian", "Polish"],
                "highlights": ["high-voltage", "heavy materials"]}

然后使用關鍵字json代替data

self.client.post('/v1/jobs/drafts/', headers=self.headers, json=data)

資源:

https://docs.locust.io/en/stable/api.html#locust.clients.HttpSession.post

將 JSON 存儲在 a.json 文件中 現在您可以直接從文件中讀取並運行測試。 為我工作:)

import json

    @task(1)
    def my_task(self):
         file_name = 'my_json.json'
         with open(file_name) as json_file:
            post_data = json.load(json_file)
            self.client.post('/v1/jobs/drafts/', data=json.dumps(post_data),
                          headers=my_headers)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM