Question about the UsersManager.getUser response from API

So, when the user exists, the API returns an array of things (strings and bool). When the user does not exist, the API returns two strings.

This seems to be a problem when unmarshalling the API response. If I unmarshall it to a variable of array of structs, it works fine when the user exists, but when the user does not exist the unmarshalling fails.

On the other hand, when I unmarshall the response to a normal struct containing strings and bools, when the user exists, the unmarshalling fails.

When user exists the response is of the format:

[
    {
        "login": "testuser",
        "alias": "testuser",
        "email": "testuser@test.com",
        "superuser_access": "0",
        "date_registered": "2019-03-19 09:00:00",
        "uses_2fa": false
    }
]

but when the user doesn’t exist it is

{
    "result": "error",
    "message": "User 'testuser' doesn't exist."
}

Shouldn’t the first response be encapsulated inside a { }?

Hi,

Basically, what I wrote here also applies in this case.

Of course on could argue that if a user is found, this user should be returned:

{
  "login": "testuser",
  "alias": "testuser",
  "email": "testuser@test.com",
  "superuser_access": "0",
  "date_registered": "2019-03-19 09:00:00",
  "uses_2fa": false
}

but changing this would be a breaking change.

Hi,

Let’s say you want to read the response. What would be the struct structure so that the mapping is done correctly in both cases where the user exists and doesn’t exist.

I don’t have any experience with languages where the JSON response has to be predefined. I’d write something similar to this in Python (hacked together quickly):

import requests

token = "THETOKEN"

username = "test"
# username = "admin"

url = f"http://dev.matomo/index.php?module=API&method=UsersManager.getUser&userLogin={username}&format=JSON&token_auth={token}"

r = requests.get(url)
response = r.json()

if "result" in response:
    print("An error occured:")
    print(response["message"])
else:
    user = response[0]
    print(user["email"])