0

Can someone explain me why I'm getting 422 error?

I have JSON from Postman and now I'm trying to send this JSON in Locust. BUt all the time I'm getting 422 error.

This is JSON from Postman:

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"]}

With lowercase boolean I have prompts to create parameters, when I change to uppercase I have 422.

This is how i want to post my JSON

self.client.post('/v1/jobs/drafts/', headers=self.headers, data=json.dumps(data))
margaret
  • 123
  • 2
  • 7

2 Answers2

0

This should work:

First create your json as a dictionary with python syntax:

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"]}

Then use the keyword json instead of data

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

source:

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

mama
  • 2,046
  • 1
  • 7
  • 24
0

Store the JSON in a .json file Now you can read directly from the file and run your tests. Works for me :)

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)