Api error on create user ruby

Summary

Getting a api.context.invalid_body_param.app_error on Post request to create user.

Steps to reproduce

Running my code with a random email and username
Ruby Version: 2.3.1
Mattermost Version: 5.7.0
API: V4

Expected behavior

Creation of a user.

I am trying to create a User via a ruby script in which username email and password are all generated. I am using HTTParty for the api requests and post JSON formated parameters. The variables all have the correct values for username an email attached to them.

creation_hash.each do |account_creator|
pass = generate_password(12)
creation = HTTParty.post(‘http://XXXXXX:8065/api/v4/users’,
headers: {
“Authorization”: “Bearer gcyyj4w1tfgq7yxkmtt4cmpg3y”,
“cache-control”: “no-cache”,
“Content-Type”: “application/json”
},
body: {
“email”: “#{account_creator[1].first}”,
“username”: “#{account_creator[0].first}”,
“password”: “#{pass}”
}
)
end

Observed behavior

Following Error Code:
{“id”:“api.context.invalid_body_param.app_error”,“message”:“Invalid or missing user in request body”,“detailed_error”:"",“request_id”:“ID”,“status_code”:400}

I am unsure maybe there is an error in my HTTParty post request but i am pretty sure i am sending valid JSON to the API.

Thanks in advance.

Hi @Vash90,

A response from a community member:

Don’t quote the keys in dictionaries. (And since "username" differs from username Mattermost is unable to create an user from the posted json.)

Hi @amy.blais.

Firstly thanks for your reply :slight_smile: . I actually managed to find another solution. I just had to add a .to_json to the body to convert the body into json (not sure why i had to though since it should allready have done so).

Also if your solution works the docs should be updated since the api docs cleary show the username in dictionaries.