Claus Fuss
Claus Fuss

Reputation: 3

400 Bad Request POST request

I'm programing in Python some API application, using POSTMAN, and a Bearer token. I already receive the token, and to some GET with success response.

But when doing a insert of a record I got 400 Bad request error, this is the code I'm using for adding the record

def add_identity(token, accountid, newIdentity):
    end_point = f"https://identityservice-demo.clearid.io/api/v2/accounts/{accountid}/identities/"
    headers = CaseInsensitiveDict()
    headers["Content-type"] = "application/json; charset=utf-8"
    headers["Authorization"] = f"Bearer {token}"

    response = requests.request("POST", end_point, data=newIdentity, headers=headers)
    print(f"{response.reason} - {response.status_code}")

the variable newIdentity has the following data

nID = {
        "privateData": {
            "birthday": "1985-30-11T18:23:27.955Z",
            "employeeNumber": "99999999",
            "secondaryEmail": "",
            "cityOfResidence": "Wakanda",
            "stateOfResidence": "Florida",
            "zipCode": "102837",
            "phoneNumberPrimary": "(999)-999-999)",
            "phoneNumberSecondary": "+5-(999)-999-9999"
        },
        "companyData": {
            "approvers": [
                {
                    "approverId": ""
                }
            ],
            "supervisorName": "Roger Rabbit",
            "departmentName": "Presidency",
            "jobTitle": "President",
            "siteId": "string",
            "companyName": "ACME Inc",
            "workerTypeDescription": "",
            "workerTypeCode": ""
        },
        "systemData": {
            "hasExtendedTime": "true",
            "activationDateUtc": "2022-03-16T18:23:27.955Z",
            "expirationDateUtc": "2022-03-16T18:23:27.955Z",
            "externalId": "999999",
            "externalSyncTimeUtc": "2022-03-16T18:23:27.955Z",
            "provisioningAttributes": [
                {
                    "name": ""
                }
            ],
            "customFields": [
                {
                    "customFieldType": "string",
                    "customFieldName": "SSNO",
                    "customFieldValue": "9999999"
                }
            ]
        },
        "nationalIdentities": [
            {
                "nationalIdentityNumber": "0914356777",
                "name": "Passport",
                "issuer": "Wakanda"
            }
        ],
        "description": "1st Record ever",
        "status": "Active",
        "firstName": "Bruce",
        "lastName": "Wayne",
        "middleName": "Covid",
        "displayName": "Bruce Wayne",
        "countryCode": "WK",
        "email": "[email protected]",
        "creationOnBehalf": "ACME"
    }

what could solve the problem? the swagger for the API is

https://identityservice-demo.clearid.io/swagger/index.html#/Identities/get_api_v2_accounts__accountId__identities

Thanks for your help in advance

Upvotes: 0

Views: 1004

Answers (1)

ApyGuy
ApyGuy

Reputation: 13

data have to be a dict ,, you can try import json and data=json.dumps(newIdentity) , and if it keeps returning 400 , check well that all the parameters are accepted by the api by recreating the request with Postman or any request editor, and if the api uses any web interface check what is the reason for that 400 . This was translated by Google so I don't know if I said something nonsense :)

Upvotes: 1

Related Questions