New Member
Posts: 1
Registered: ‎11-07-2017

Api question. Unable to create a user

[ Edited ]

Greetings everyone,

I recently encountered a problem with creating a user via UCRM Api. I was using examples listed on apiary, that do technically work (i get a 200 responce, but no user is created).

Here is python 3.6 code:

 

import requests
import json

api_token='the apikey'
api_url_base='http://mysite.mysite.com/api/v1.0/clients'

headers={
'Content-Type': 'application/json',
'X-Auth-App-Key': api_token
}

parameters={
"userIdent": "qweqeqweq",
"previousIsp": "null",
"isLead": False,
"clientType": 1,
"companyName": "null",
"companyRegistrationNumber": "null",
"companyTaxId": "null",
"companyWebsite": "null",
"companyContactFirstName": "null",
"companyContactLastName": "null",
"firstName": "Sasdoddffdos",
"lastName": "Spsdopis",
"street1": "2580 Orchaasdfrd Pkwy",
"street2": "null",
"city": "San Jose",
"countryId": 249,
"stateId": 1,
"zipCode": "95131",
"invoiceStreet1": "2580 Orchard Pkwy",
"invoiceStreet2": "null",
"invoiceCity": "San Jose",
"invoiceStateId": 1,
"invoiceCountryId": 249,
"invoiceZipCode": "95131",
"invoiceAddressSameAsContact": True,
"note": "test",
"sendInvoiceByPost": True,
"invoiceMaturityDays": 3,
"stopServiceDue": True,
"stopServiceDueDays": 2,
"organizationId": 1,
"tax1Id": 1,
"tax2Id": 2,
"tax3Id": 3,
"registrationDate": "2126-09-12T00:00:00+0000",
"username": "soasdasdssdirnya",
"avatarColor": "#FFAAAA",
"addressGpsLat": 37.383255,
"addressGpsLon": -121.931131,
"contacts": [
{
"email": "spoapss@examdsdsple.com",
"phone": "82283591488",
"name": "ass2",
"isBilling": False,
"isContact": False,
"types": [
{
"name": "hasduynya"
}
]
}
],
"attributes": [
{
"value": "attr-value",
"customAttributeId": "null"
}
],
"password": "qwe"
}

response=requests.post(api_url_base, params=parameters, headers=headers)

print(response.status_code)
print(response.content)

 

The response i get is

  200
   b'[]'

 

I would gladly hear what do you think about this, and if it is possible to somehow fix this issue.

Best of all to you.

Ubiquiti Employee
Posts: 3,923
Registered: ‎12-10-2015
Kudos: 1381
Solutions: 299

Re: Api question. Unable to create a user

we will look into it. Please, which UCRM version do you have?
Highlighted
Ubiquiti Employee
Posts: 5
Registered: ‎01-16-2018
Solutions: 2

Re: Api question. Unable to create a user

[ Edited ]

Hello @Notespeak

It looks like your JSON is not valid:

 

"key": True,
"key": False

is not valid syntax, it should be

"key": true,
"key": false

I'm suggesting to use some validator, for example https://jsonlint.com/

 

If I POST your JSON on /api/v1.0/clients, I'm reciving

{
    "code": 400,
    "message": "Invalid json message received"
}

not 200 OK

 

After further validation of JSON, endpoint works correctly, using UCRM stable version 2.14.7 or online demoimage.png

Member
Posts: 295
Registered: ‎11-18-2009
Kudos: 77
Solutions: 8

Re: Api question. Unable to create a user

I would the json library that you have already imported to it's full potential...

 

...

# After the declaration of parameters={...}

# Convert that data into a python object...
parameters_encoded = json.loads(parameters)

# Then pass the parameters_encoded object to the requests.post() method.
response=requests.post(api_url_base, params=parameters_encoded, headers=headers)

...

# You can then also do the reverse, once you have the response, if you like.
response_decoded = json.dumps(response.content)

# Leaving you with a Python object again!

...

 

I did not throw your code in my Python IDE, but those simple additions should fix your issue.  Believe it or not, I even have to coerce the actual JSON objects once in a while to make it valid.  In this case though, better to match the API/JSON standards.

 

Let me know if you need more assistance.