Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create_user returns 400 error #44

Closed
ard9000 opened this issue Apr 17, 2020 · 11 comments
Closed

create_user returns 400 error #44

ard9000 opened this issue Apr 17, 2020 · 11 comments
Assignees
Labels

Comments

@ard9000
Copy link

ard9000 commented Apr 17, 2020

All of the other API calls work fine.

HTTPError: 400 Client Error: Bad Request for url: https://chat.mychatserver.com/api/v1/users.create

@pipozzz
Copy link
Member

pipozzz commented Apr 17, 2020

Could you give us more info about your environemnt? What version of rocket.chat you are using ? What payload you are sending ?

@mmehrle
Copy link

mmehrle commented Apr 17, 2020

Hi there - I work with ard900. We are both using OS X and installed the latest version of rocket-python (fresh install via pip).

Here's the code (with passwords and URLs altered):

from rocketchat.api import RocketChatAPI

def send_rc_message(api, message):
    api.send_message(message, 'expected_move_breakouts')

def init_rocket_chat():
    api = RocketChatAPI(settings={'username': 'xxx@xxxxxxxx.com', 'password': 'XXXXXXX',
                                    'domain': 'https://chat.xxxxxxxxx.com'})
    return api

api = init_rocket_chat()

def add_user(api, email, name, password, username):
    api.create_user('email', 
                    'name', 
                    'password', 
                    'username', 
                     active=True, 
                     roles=['user'], 
                     join_default_channels=True, 
                     require_password_change=False, 
                     send_welcome_email=False, 
                     verified=False, 
                     customFields=None)

add_user(api, 'xxxxxx@gmail.com','Ard9','12344321XX','secret-password')

Output:


HTTPError Traceback (most recent call last)
in ()
----> 1 add_user(api, 'xxxxxx@gmail.com','Ard9','12344321XX','secret-password')

in add_user(api, email, name, password, username)
11 send_welcome_email=False,
12 verified=False,
---> 13 customFields=None)

/Users/michael/anaconda3/lib/python3.6/site-packages/rocketchat/api.py in create_user(self, email, name, password, username, **kwargs)
220 password=password,
221 username=username,
--> 222 **kwargs
223 )
224

/Users/michael/anaconda3/lib/python3.6/site-packages/rocketchat/calls/base.py in call(self, *args, **kwargs)
115 ))
116
--> 117 result.raise_for_status()
118 self.logoff()
119

/Users/michael/anaconda3/lib/python3.6/site-packages/requests/models.py in raise_for_status(self)
938
939 if http_error_msg:
--> 940 raise HTTPError(http_error_msg, response=self)
941
942 def close(self):

HTTPError: 400 Client Error: Bad Request for url: https://chat.XXXXXXX.com/api/v1/users.create

@pipozzz
Copy link
Member

pipozzz commented Apr 17, 2020

First problem which I see is using constant string instead of function parameters here:

def add_user(api, email, name, password, username):
    api.create_user('email',  # <- here
                    'name',  # <- here
                    'password', # <- here
                    'username', # <- here
                     active=True, 
                     roles=['user'], 
                     join_default_channels=True, 
                     require_password_change=False, 
                     send_welcome_email=False, 
                     verified=False, 
                     customFields=None)

@mmehrle
Copy link

mmehrle commented Apr 17, 2020

Yeah I can't believe we both didn't see that - LOL - that's what copy/paste gets you :-)

Fixed it:

def add_user(api, email, name, password, username):
    api.create_user(email, 
                    name, 
                    password, 
                    username, 
                    active=True, 
                    roles=['user'], 
                    join_default_channels=True, 
                    require_password_change=False, 
                    send_welcome_email=False, 
                    verified=False, 
                    customFields=None)

but it's still throwing the same error for some reason:


HTTPError Traceback (most recent call last)
in ()
----> 1 add_user(api, 'xxxxx@gmail.com','xxxx','xxxxxx','xxxxx')

in add_user(api, email, name, password, username)
10 send_welcome_email=False,
11 verified=False,
---> 12 customFields=None)

/Users/michael/anaconda3/lib/python3.6/site-packages/rocketchat/api.py in create_user(self, email, name, password, username, **kwargs)
220 password=password,
221 username=username,
--> 222 **kwargs
223 )
224

/Users/michael/anaconda3/lib/python3.6/site-packages/rocketchat/calls/base.py in call(self, *args, **kwargs)
115 ))
116
--> 117 result.raise_for_status()
118 self.logoff()
119

/Users/michael/anaconda3/lib/python3.6/site-packages/requests/models.py in raise_for_status(self)
938
939 if http_error_msg:
--> 940 raise HTTPError(http_error_msg, response=self)
941
942 def close(self):

HTTPError: 400 Client Error: Bad Request for url: https://chat.xxxxxxxx.com/api/v1/users.create

@mmehrle
Copy link

mmehrle commented Apr 18, 2020

Hi again - it seems you may have misunderstood my latest response. The problem persists even though I fixed the params. Can you please take another look?

@pipozzz
Copy link
Member

pipozzz commented Apr 19, 2020

Just change customFields=None to customFields={} it seems new version of rocketchat do not support None type, I will fix this today or so.

@pipozzz pipozzz self-assigned this Apr 19, 2020
@pipozzz pipozzz added the bug label Apr 19, 2020
@pipozzz
Copy link
Member

pipozzz commented Apr 19, 2020

It's already fixed so from now you can avoid setting customFields if you don't use it. If you have any problem just reopen this issue or create new one.
Thanks

@pipozzz pipozzz closed this as completed Apr 19, 2020
@mmehrle
Copy link

mmehrle commented Apr 19, 2020

Yes that worked - thanks a lot for fixing this!

@warriorbik
Copy link

I've the similar problem when using the create_user api.

api.create_user('xxxx@kodiary.com','xxx','xxxx','xxxx', active=True, roles=['user'],join_default_channels=False, require_password_change=False, send_welcome_email=True, verified=False, customFields={})
The error is same when removing customFields and using customFields=None.
HTTPError: 400 Client Error: Bad Request for url: https://chat.kodiary.com/api/v1/users.create

Where as from the logs of rocket chat on my server shows:

{"level":35,"time":"2022-02-18T05:09:11.600Z","pid":8,"hostname":"036b252e8186","name":"","name":"API","method":"POST","url":"/api/v1/login","userAgent":"python-requests/2.27.1","length":"29","host":"chat.kodiary.com","remoteIP":"27.34.22.180","responseTime":159}
{"level":35,"time":"2022-02-18T05:09:12.778Z","pid":8,"hostname":"036b252e8186","name":"","name":"API","method":"POST","url":"/api/v1/users.create","userId":"u7nopRH2ZfBieBk83","userAgent":"python-requests/2.27.1","length":"256","host":"chat.kodiary.com","remoteIP":"27.34.22.180","err":{"type":"errorClass","message":"Match error: Missing key 'email'","stack":"Error: Match error: Missing key 'email'
at check (packages/check/match.js:36:17)
at Object.post (app/api/server/v1/users.js:36:4)
at app/api/server/api.js:425:96
at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1257:12)
at Object._internalRouteActionHandler [as action] (app/api/server/api.js:425:39)
at Route._callEndpoint (packages/rocketchat_restivus/lib/route.coffee:150:32)
at packages/rocketchat_restivus/lib/route.coffee:59:33
at packages/simple_json-routes.js:100:9","path":"","sanitizedError":{"type":"errorClass","message":"Match failed [400]","stack":"Error: Match failed [400]
at errorClass. (packages/check/match.js:91:27)
at new errorClass (packages/meteor.js:660:17)
at check (packages/check/match.js:36:17)
at Object.post (app/api/server/v1/users.js:36:4)
at app/api/server/api.js:425:96
at Meteor.EnvironmentVariable.EVp.withValue (packages/meteor.js:1257:12)
at Object._internalRouteActionHandler [as action] (app/api/server/api.js:425:39)
at Route._callEndpoint (packages/rocketchat_restivus/lib/route.coffee:150:32)
at packages/rocketchat_restivus/lib/route.coffee:59:33
at packages/simple_json-routes.js:100:9","isClientSafe":true,"error":400,"reason":"Match failed","errorType":"Meteor.Error"},"errorType":"Match.Error"},"status":400,"responseTime":11,"msg":"Match error: Missing key 'email'"}
{"level":35,"time":"2022-02-18T05:09:18.556Z","pid":8,"hostname":"036b252e8186","name":"","name":"API","method":"POST","url":"/api/v1/method.call/license:getModules","userId":"wyQNrrLLPaWRoTsDE","userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36","length":"96","host":"chat.kodiary.com","remoteIP":"27.34.22.180","status":200,"responseTime":4}

@opensworup
Copy link

@pipozzz Can you update the documentation to create new users. Right now it has customFields=None. It should be customFields={} .

@pipozzz
Copy link
Member

pipozzz commented Aug 21, 2023

Thanks for letting me know, I'll fix this soon

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants