Coveo REST Search API Using Post Method

Coveo REST Search API Using Post Method

Coveo Rest API is a simple REST interface for invoking, exposing and manipulating the search based on the different filtering and search criteria.

Coveo provide both cloud and on-permises indexing Where Coveo cluod customer can accessed the REST API throught Coveo Cloud Plateform. But for on-permise indexing, we need to deploy the Search API locally. We are using the Coveo Cloud Platform and this blog is based on it. And when we are taking about the Coveo Cloud Platform then REST API is available to all users through a single URL – https://cloudplatform.coveo.com/rest/search/v2

Coveo provide many means of client authentication, but we will use the Authorization Key for the authenticating the client. We can perform both GET and POST method for the search. We can pass multiple supporting parameter either in the query string or in the body of the POST method as JSON format.

A typical POST method example in post man for the coveo cloud plateform is as follow –

postman

Where POST URL is https://cloudplatform.coveo.com/rest/search/v2, and authorization is done using Authentication key. Different parameters for the search such as organizationid, query, advance query etc. are passed in the body as content type – application/json.

A C# static class for making query to the coveo cloud plate form is as follow –

using System;

using System.IO;

using System.Net;

using System.Text;

using Sitecore.Diagnostics;

using MyStringExtentions

 

namespace MyNameSpace

{

Public static class MakeCoveoRequest

{

public static string MakeRequest(string query)

{

var result = string.Empty;

var CoveoRestUrl = “https://cloudplatform.coveo.com/rest/search/v2”;

 

var coveoUri = new Uri(CoveoRestUrl);

 

var httpWebRequest = WebRequest.Create(coveoUri) as HttpWebRequest;

if (httpWebRequest == null) return result;

 

try

{

var postData = Encoding.UTF8.GetBytes(query);

httpWebRequest.Method = “POST”;

httpWebRequest.PreAuthenticate = true;

httpWebRequest.Headers.Add(“Authorization”, “Bearer MyAuthorizationKey”);

httpWebRequest.ContentType = “application/json”;

httpWebRequest.ContentLength = postData.Length;

using (var dataStream = httpWebRequest.GetRequestStream())

{

dataStream.Write(postData, 0, postData.Length);

}

var myWebResponse = httpWebRequest.GetResponse();

var responseStream = myWebResponse.GetResponseStream();

if (responseStream == null) return null;

var myStreamReader = new StreamReader(responseStream, Encoding.Default);

result = myStreamReader.ReadToEnd();

responseStream.Close();

myWebResponse.Close();

}

catch (WebException ex)

{

Log.Error(“Message” + ex.Message, ex);

}

 

return result;

}

}

}

 

Thanks

 

Advertisements

One thought on “Coveo REST Search API Using Post Method

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s