If you download AX integration samples from GitHub, you’ll see (in JsonConsoleApplication project) that you can call JSON-based custom services by code like this:
var request = HttpWebRequest.Create(ClientConfiguration.Default.UriString + "api/services/UserSessionService/AifUserSessionService/GetUserSessionInfo"); request.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader(); request.Method = "POST"; request.GetResponse();
It will call the operation and return its return value as JSON:
{ "$id":"1", "AOSLocaleName":"en-US", "AXLanguage":"EN-US", "Company":"DAT", "CompanyTimeZone":58, "CurrencyInfo": { "$id":"2", "CurrencyCode":"USD", "Description":"US Dollar", "ExchangeRate":100.0, "ISOCurrencyCode":"USD", "Prefix":"","Suffix":"" }, "IsSysAdmin":true, "UserId":"wintermute", "UserPreferredCalendar":0, "UserPreferredTimeZone":18 }
This is what happens when you use the POST method of HTTP; if you switch to GET, you’ll actually get some information about the service.
Therefore if you merely change the value of request.Method:
var request = HttpWebRequest.Create(ClientConfiguration.Default.UriString + "api/services/UserSessionService/AifUserSessionService/GetUserSessionInfo"); request.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader(); request.Method = "GET"; request.GetResponse();
you’ll get a very different response:
{ "Parameters":[], "Return": { "Name":"return", "Type":"AifUserSessionInfo" } }
You can see that the operation doesn’t expect any parameters and it returns a single object of AifUserSessionInfo type. This gives you some limited information about how to use this service.
You can also use GET requests to discover existing services and their operations.
/api/services gives you a list of all service groups:
{"ServiceGroups":[{"Name":"AxClient"},…,{"Name":"UserSessionService"}…]}
/api/services/UserSessionService provides a list of services in the service group:
{"Services":[{"Name":"AifUserSessionService"}]}
/api/services/UserSessionService/AifUserSessionService shows all operations of the individual service:
{ "Operations": [ {"Name":"ApplyTimeZone"}, {"Name":"GetAccessRights"}, {"Name":"GetPartitionKey"}, {"Name":"GetPartitionKeysForUser"}, {"Name":"GetUserSessionInfo"}, {"Name":"IsSinglePartitionSystem"}, {"Name":"RemoveTimeZone"} ] }
Don’t forget than opening an URL in browser makes a GET request, therefore you don’t have to write any code to get this kind of information about custom services.
But maybe you would like something a bit more sophisticated, which is the topic of the next post: Open API for JSON-based custom services in AX 7.