You are on page 1of 7

Content Server - Web Services - How to use Search API with REST authentication

Technical Article applies Content Server SDK (Builder, LAPI, Java, Web Services)
to: [ 10.0 10.5 ]

Summary: In Content Server, how can you authenticate using REST API and
use Search API?

Error Message:

Resolution: The idea is to extract the token that is returned by the Authenticate
Method and inject it into your Search HTTP request. The
following is an example of what can be done:

static void Main(string[] args)

string token =
REST_Authenticate(username, password);

//Create a new HttpWebRequest object to


run the search. Set the correct hostname and path
for your environment in the URI object.

HttpWebRequest searchRequest =
(HttpWebRequest)WebRequest.Create(new
Uri(yourCSserver));

//Set up the request parameters for the


search POST. Adjust or add parameters as desired.

var postData2 = "func=search";

postData2 += "&where1="+ filename2search;

postData2 += "&outputformat=xml";
var searchPost =
Encoding.ASCII.GetBytes(postData2);

//Set up the required request parameters


for the search POST.

searchRequest.ContentLength =
searchPost.Length;

searchRequest.ContentType =
"application/x-www-form-urlencoded";

searchRequest.Method = "POST";

searchRequest.Credentials =
CredentialCache.DefaultCredentials;

searchRequest.Headers.Add("OTCSTicket",
token);

searchRequest.CookieContainer = new
CookieContainer();

//Always set the referer to match the


FQDN of the server or the request will be rejected.

searchRequest.Referer = yourserver;

using (var stream =


searchRequest.GetRequestStream()) {

stream.Write(searchPost, 0,
searchPost.Length);

}
HttpWebResponse searchResponse =
(HttpWebResponse)searchRequest.GetResponse();

//Stream search results to string, ready


to parse.

Stream dataStream =
searchResponse.GetResponseStream();

StreamReader reader = new


StreamReader(dataStream);

string responseFromServer =
reader.ReadToEnd();

//Output to console for sanity checking.

Console.WriteLine(responseFromServer);

//Close off remaining objects.

searchResponse.Close();

reader.Close();

dataStream.Close();

Console.ReadKey();
}

/// <summary<

/// Authenticates to the given yourCSserver

/// </summary>

static string REST_Authenticate(string


g_username, string g_password)

string token;

//example : http://<<server
name>>:port/<<path>>/api/<<version>>/auth

string contentURL = yourCSserver +


"/api/v1/auth";

string payload = "username=" + g_username


+ "&password=" + g_password;

byte[] byteArray =
Encoding.UTF8.GetBytes(payload);

var request =
(HttpWebRequest)HttpWebRequest.Create(contentURL);

request.Method = "POST";

request.ContentType = "application/x-www-
form-urlencoded";
request.ContentLength = byteArray.Length;

CookieContainer cookieContainer = new


CookieContainer();

request.Credentials =
CredentialCache.DefaultCredentials;

// send payload data to server

using (var stream =


request.GetRequestStream()) {

stream.Write(byteArray, 0,
byteArray.Length);

//get HTTP response

using (var response =


(HttpWebResponse)request.GetResponse()) {

var responseValue = string.Empty;

if (response.StatusCode !=
HttpStatusCode.OK) {

var message =
String.Format("Request failed. Received HTTP {0}",
response.StatusCode);

throw new
ApplicationException(message);
}

String json = new


StreamReader(response.GetResponseStream()).ReadToEnd(
);

// extract token from JSON. We have


used a custom class to serialize a JSON

var serializer = new


JavaScriptSerializer();

serializer.RegisterConverters(new[] {
new DynamicJsonConverter() });

dynamic obj =
serializer.Deserialize(json, typeof(object));

token = obj.ticket;

return token;

Attachments:

Other Keywords: SEARCH, API, REST, CWS

You might also like