JSON API v8 - add contact with python fail

Hi,
I am trying to add contact using JSON api. I am not a python master but I was able to make it work partially. I am able to read without any problems (so tokens, setup, auth is not an issue I think).
But I can’t create conact with below code. I get ‘Bad data passed in’
Can somebody help me find an error? I already spent hours trying to solve it.

suitecrm_contact_data = {
        'data':  {
            'type': 'Contacts',
            'attributes': {
                'last_name': 'Test',
                "first_name": "test2",
            }
        }  
}


suitecrm_api_url = 'https://url/Api/V8/module'
suitecrm_api_url_get = 'https://url/Api/V8/module/Contacts'

response = requests.get(
            suitecrm_api_url_get,
            headers={'Authorization': f'Bearer {token['access_token']}','Content-type':'application/x-www-form-urlencoded'},
            verify=False 
        )
print(response.content)
#I do get the contacts as requested - no problem in auth
response = requests.post(
            suitecrm_api_url,
            json=suitecrm_contact_data,
            headers={'Authorization': f'Bearer {token['access_token']}','Content-type':'application/x-www-form-urlencoded'},
            verify=False 
        )
#here I do get 
b'Bad data passed in; <a href="https://url/">Return to Home</a>'

I am running fresh 7.14.2

Would you be able to set your POST Header 'Content-type' : 'application/json' and try again? Let us know if this solves your issue.

1 Like

Argh! It does solve the issue!!!
Thanks a lot.

As there are not many examples I am pasting sample working script for python with auth, token handling, listing and adding record. Maybe it will help somebody

import requests
import hashlib
h = hashlib.sha256()
from urllib3.exceptions import InsecureRequestWarning
from urllib3 import disable_warnings
disable_warnings(InsecureRequestWarning)

def authenticate_suitecrm(base_url, client_id, client_secret):
    auth_url = f'{base_url}/Api/access_token'
    data = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret
    }
    headers = { 
        'Content-type: application/json',
        'Accept: application/json'
    }

    response = requests.post(auth_url, data=data ,verify=False)
    
    if response.status_code == 200:
        return response.json()  # Returns the access token and other details
    else:
        raise Exception(f"Failed to authenticate: {response.text}")

# Usage
base_url = 'https://url'  # Replace with your SuiteCRM instance URL
client_id = '6b4570af-9c58-d0d1-6360-6592ad0fbfe7'  # Replace with your client ID
client_secret = 'secret'  # Replace with your client secret

token = authenticate_suitecrm(base_url, client_id, client_secret)
print("Token", token)

suitecrm_contact_data = {
        'data':  {
            'type': 'Contacts',
            'attributes': {
                'last_name': 'Test',
                'first_name': "test2",
            }
        }  
}



suitecrm_api_url = 'https://url/Api/V8/module'
suitecrm_api_url_get = 'https://url/Api/V8/module/Contacts'

#printing all contact just to see if auth works
response = requests.get(
            suitecrm_api_url_get,
            headers={'Authorization': f'Bearer {token['access_token']}','Content-type':'application/json'},
            verify=False 
        )
print(response.request.headers)
print(response.content)
#lest add sample data
response = requests.post(
            suitecrm_api_url,
            json=suitecrm_contact_data,
            headers={'Authorization': f'Bearer {token['access_token']}','Content-type':'application/json'},
            verify=False 
        )
print(response.request.headers)
print(response.content)
4 Likes

Thank you very much for sharing your working python code script! This will be incredibly time saving for the 1/3 of all software developers who are python programmers! Amazing fact of the day!

1 Like