jbloomer / SnipeIT-PythonAPI

Python Interface to the SnipeIT (https://github.com/snipe/snipe-it) API
80 stars 37 forks source link

Exception creating new user #9

Closed ghost closed 5 years ago

ghost commented 5 years ago

Hi, I have had this working before, so perhaps I've done something wrong, but when I try to create a new user (even stepping through the example from UsersPostRequestExample.py), I get the error simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

I've checked & double checked the payload and copied the payload from the example, but I always get the error. I'm connected fine to the server as I can retrieve users. I tried updating everything (and then the typo fix on Assets.py) but I still get the exception. I also tried passing in both text and a dynamic 'object'.

Any ideas what I'm doing wrong? Thanks, Darren.

jbloomer commented 5 years ago

It is kind of hard to answer your question without more information. Is it possible for you to share your script with me? If so I advise you sterilize it to not reveal and private or company information.

ghost commented 5 years ago

Hi, thanks for your help.

As I mentioned even when I run the create user sample I get the error, but here is a script that fails.

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import json
from snipeit import Users

server='https://snipe.domain.nz'
pw = '<our token>'

U = Users()
snipe_users = json.loads(U.get(server, pw, limit=500))['rows']
print('Found ' + str(len(snipe_users)) + ' Snipe users')

newuser = '{"username":"dchtest1","first_name":"Darren","last_name":"Harrison","email":"my.email@domain.com","password":"new password"}'
r = U.create(server, pw, newuser)

print('Added user', r)

The output from running that is:

Found 52 Snipe users
...
File "/home/user/xDrive/Infrastructure/Ansible/snipe-test.py", line 18, in <module>
    r = U.create(server, pw, newuser)
  File "/home/user/.local/lib/python2.7/site-packages/snipeit/Users.py", line 93, in create
    return json.dumps(results.json(),indent=4, separators=(',', ':'))
  File "/home/user/.local/lib/python2.7/site-packages/requests/models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)
  File "/home/user/.local/lib/python2.7/site-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/home/user/.local/lib/python2.7/site-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/home/user/.local/lib/python2.7/site-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
jbloomer commented 5 years ago

This is the create method you are calling

def create(self, server, token, payload):
        """Create new user data.

        Arguments:
            server {string} -- Server URI
            token {string} -- Token value to be used for accessing the API
            payload {string} -- input parameters

        Returns:
            string -- server response in JSON format
        """
        self.uri = '/api/v1/users'
        self.server = server + self.uri
        headers = {'Content-Type': 'application/json','Authorization': 'Bearer ' + token}
        results = requests.post(self.server, headers=headers, data=payload)
        return json.dumps(results.json(),indent=4, separators=(',', ':'))

Can you check in the web console to see if the new user actually exist even if you get this error? I suspect the server is not returning JSON and the return the method is doing is trying to load JSON which would cause the error.

ghost commented 5 years ago

Thanks for your help, I think I've found the issue. When I make the call from the command line I am getting a 302 redirect, and no, the user isn't created. The GET works fine (and all the other commands) but POST doesn't. Weird.

So I guess that means it's an issue with Snipe itself, or our setup, so I'll follow up there.

Thanks for creating the Snipe Python API.

Darren.