Using the HTTP Interface

The HTTP interface allows applications to integrate translation functionality by invoking HTTP GET & POST method. The interface is technology agnostic; no particular operating system or programming language is needed to develop using it. The HTTP API uses the same protocols and verbs as the World Wide Web and is simpler than AJAX.

The Translate Method receives all of its parameters via GET. The TranslateArray Method (as well as other Array methods) receives parameter values via POST. The code below illustrates how to use the HTTP interface of Microsoft Translator using the Translate Method.

To make request to any method of Microsoft translator you will need access token. Process of obtaining access token is explained in detailed at Obtain Access Token. Use the value of access token in the Authorization header of the subsequent calls to the Microsoft Translator API. This Token is valid for 10 minutes. Authorization header value should be in following format. (Note: One BLANK space between Bearer and Access Token Value)

"Bearer" + " " + Access Token Value

In C#, you can use HttpWebRequest to make request.


                                
string text = "Use pixels to express measurements for padding and margins.";
string from = "en";
string to = "de";

string uri = "http://api.microsofttranslator.com/v2/Http.svc/Translate?text=" + System.Web.HttpUtility.UrlEncode(text) + "&from=" + from + "&to=" + to;
string authToken = "Bearer" + " " + admToken.access_token;

HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Headers.Add("Authorization", authToken);

                            

In this code snippet, parameters required for translate method are provided through URL as a query string parameters.

Making HTTP request to Translate method

The code snippet below makes a request to the Microsoft Translator's Translate method.


                                
WebResponse response = null;
try
{
   response = httpWebRequest.GetResponse();
   using (Stream stream = response.GetResponseStream())
   {
     System.Runtime.Serialization.DataContractSerializer dcs = new System.Runtime.Serialization.DataContractSerializer(Type.GetType("System.String"));
     string translation = (string)dcs.ReadObject(stream);
     Console.WriteLine("Translation for source text '{0}' from {1} to {2} is", text, "en", "de");
     Console.WriteLine(translation);
   }
}
                            

For all available HTTP method reference

Show: