0

I am using the following code to get all users. It works with the Java client but is giving the following error

{ code: '[invalid]', api_1 | message: 'You must specify either the [ids], [queryString], [email] or [username] property for a GET request.' }

What I am trying

    let requestJSON = {}
    requestJSON.queryString = "*";
    client.searchUsersByQueryString(requestJSON).then(handleResponse).catch(handleResponse);

    function handleResponse (clientResponse) {
        console.log(clientResponse.errorResponse.generalErrors[0]);
    }

Also for some reason, it is assuming this to be a GET Request.

I have already tried doing the same using Postman and it doesn't work. I am using FusionAuth 1.5.0 everywhere.

Chaks
  • 82
  • 2
  • 15

2 Answers2

2

The client library uses a POST request with a JSON body to perform the search. See the POST /api/user/search API documentation here https://fusionauth.io/docs/v1/tech/apis/users#search-for-users

Try this instead:

let requestJSON = {
  search: {
    queryString: "*"
  }
};


client.searchUsersByQueryString(requestJSON).then(handleResponse).catch(handleResponse);

function handleResponse (clientResponse) {
  console.log(clientResponse.errorResponse.generalErrors[0]);
}
robotdan
  • 1,022
  • 1
  • 9
  • 17
  • 1
    I don't know how I missed the "search.____". Thanks really. – Chaks Apr 04 '19 at 15:56
  • Also why did you add the additional "search" in the JSON? I really thinking in terms of API design but couldn't find any reason. – Chaks Apr 04 '19 at 15:59
  • This is a pattern we utilize for all of our APIs, in this particular case it is perhaps less useful. But for consistency, we utilize it for each API. – robotdan Apr 04 '19 at 19:18
2

Just like @robotdan said, you must include the queryString field inside a search property. Although it may not be explicit at a first glance, you can imply that by taking a look at the request body fields:

Fusion Auth User API screenshot As you can see in the 3rd row, there is a search.queryString where you should define the string you are searching, which in your case is everything (*). Also, if you want an ordered list, look at the 6th row that contains the available sort fields and in the 7th row the order of the results: asc or desc (it does not appear in the screenshot so take a look at the FusionAuth official documentation page).

Imagine you want all users whose name must contain foo at any position of them fullName, ordered by users email in descending order. Your object should be something like this:

const request = {
  search: {
    queryString: '*foo*',
    sortFields: [{ name: 'email', order: 'desc' }]
  }
}

Notice the * in the beginning and in the end: those asterisks tells the search engine that foo may have a prefix and/or a suffix (but you don't care about them).