| REST API | |
When you make a REST API call, you must authenticate the user. The following provides an example of REST API authentication using JavaScript, Java, C# and Ajax.
For more information, refer to Enabling SSL in AgilePoint Server Manager.
For more information, refer to Server Side Authentication Using WCF Endpoints for AgilePoint Server.
$.ajax({
headers: {
// To make cross-domain ajax calls.
// This is required if your front-end and back-end are in different domains.
"Access-Control-Allow-Origin": "*",
// Supply your application name.
appID: 'My Application',
// If your application is being served in multiple languages,
// Call a method that returns the selected language id, for example (en-Us).
locale: getLocale(),
// Add implementation to obtain base64 encoded value of username and password.
Authorization: "Basic "+base64encode(UserName:Password)
},
url: 'https://mydomain:9011/AgilePointServer/Admin/GetRegisterUser',
// Pass username in JSON format {userName:'Domain\UserName'}
data: JSON.stringify({ userName: 'demo3\lily.allen' }),
// default HTTP verb is post.
// If the verb is GET, you must specify it.
type: 'POST',
// Response content type. By default it is json.
contentType: "application/json",
// The default data type for the request body.
dataType: "json",
// Set async to true to make a non-blocking or asynchronous ajax call.
async: true,
// Set cache to false to make sure we are not getting the cached response.
cache: false,
success: function (data, status) {
try {
// Handle success callbacks.
} catch (e) {
}
},
error: function (xhr, status, error) {
try {
//Handle any request error message or authentication failure messages.
} catch (e) {
}
}
});
public String POSTMethod(String URI, String jsonRequestData) {
org.apache.http.impl.client.CloseableHttpClient httpClient = null;
org.apache.http.client.methods.CloseableHttpResponse response = null;
httpClient = HttpClients.createDefault();
org.apache.http.client.methods.HttpPost httppost = null;
httppost = new org.apache.http.client.methods.HttpPost(URI);
httppost.addHeader("content-type", "application/json;charset=UTF-8");
httppost.addHeader("Authorization",
"Basic "+ new String(Base64.encodeBase64((Username:password).getBytes())));
httppost.addHeader("AppName", "My Application");
httppost.addHeader("Locale", Locale.getDefault());
try {
org.apache.http.entity.StringEntity se =
new org.apache.http.entity.StringEntity(jsonRequestData,
ContentType.create("text/plain", "UTF-8"));
httppost.setEntity(se);
response = httpClient.execute(httppost);
return org.apache.http.util.EntityUtils.toString(response.getEntity());
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
public HttpWebRequest GetHttpRequest(string URI, string Method)
{
//Create a request with required URI
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URI);
request.Method = Method;
//Set Content Type
request.ContentType = "application/json";
//Set Accept Type
request.Accept = "application/json";
//Setting Header
//Creating Authorization header format (Basic (base64(domain\\username:password))
request.Headers[HttpRequestHeader.Authorization] =
"Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(domain + "\\" + userName + ":" + password));
request.Headers["appID"] = appID;
request.Headers["locale"] = locale;
request.Timeout = 100000;
request.KeepAlive = false;
ServicePointManager.ServerCertificateValidationCallback += new System.Net.Security.RemoteCertificateValidationCallback(ValidateServerCertificate);
return request;
}