Code behind request

Does anybody know how to setup a VB.NET HttpWebRequest POST to SDL's API?  I would like to not use the http POST, but use a code behind POST.  Is there a way to do this?  How would you set the key, and the values.

Parents
  • API documentation for SDL Language Cloud can be found at https://languagecloud.sdl.com/translation-toolkit/api-documentation

    To get started, it’s best to sign up and an SDL Language Cloud account at https://languagecloud.sdl.com/translation-toolkit/sign-up

    Once you’re signed up and logged in, navigate to the profile page (here) and check the “Developer Capabilities” box which will give access to the Sandbox environment which can be turned on and off from the user menu (top right). Sandbox allows you to freely develop your application/integration with SDL Language Cloud without having an active subscription. Integrations generally use an “API Key” which can be generated from the Integrations menu on the left. Note that API keys are either Sandbox or Production keys depending on whether you are in Sandbox mode when you create them.

    Here’s a .NET example of a text translation call:

    namespace LCTestApp

    {

        using System;

        using System.Net.Http;

        using System.Net.Http.Headers;

        using System.Runtime.Serialization.Json;

        using System.Text;

        using System.Xml;

        using System.Xml.Linq;

        using System.Xml.XPath;

     

        class Program

        {

            static void Main(string[] args)

            {

                string apiKey = "[Your API Key Here]";

                string sourceText = "Hello Developers";

                string sourceLanguage = "eng";

                string targetLanguage = "fra";

                string json = string.Format(@"{{""text"":""{0}"",""from"":""{1}"",""to"":""{2}""}}", sourceText, sourceLanguage, targetLanguage);

                var client = new HttpClient();

                var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://lc-api.sdl.com/translate");

                requestMessage.Headers.Add("Accept", "application/json");

                requestMessage.Headers.Add("Authorization", string.Format("LC apiKey={0}", apiKey));

                requestMessage.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(json));

                requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=UTF-8");

                HttpResponseMessage response = client.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead).Result;

                if (response.IsSuccessStatusCode)

                {

                    byte[] responseBytes = response.Content.ReadAsByteArrayAsync().Result;

                    XmlReader reader = JsonReaderWriterFactory.CreateJsonReader(responseBytes, new XmlDictionaryReaderQuotas());

                    XElement root = XElement.Load(reader);

                    XElement translation = root.XPathSelectElement("//translation");

                    Console.WriteLine(translation.Value);

                }

                else

                {

                    Console.WriteLine("#ERROR: " + Encoding.UTF8.GetString(response.Content.ReadAsByteArrayAsync().Result));

                }

     

                Console.ReadKey();

            }

        }

    }

    David Pooley | Senior Product Manager | SDL

Reply
  • API documentation for SDL Language Cloud can be found at https://languagecloud.sdl.com/translation-toolkit/api-documentation

    To get started, it’s best to sign up and an SDL Language Cloud account at https://languagecloud.sdl.com/translation-toolkit/sign-up

    Once you’re signed up and logged in, navigate to the profile page (here) and check the “Developer Capabilities” box which will give access to the Sandbox environment which can be turned on and off from the user menu (top right). Sandbox allows you to freely develop your application/integration with SDL Language Cloud without having an active subscription. Integrations generally use an “API Key” which can be generated from the Integrations menu on the left. Note that API keys are either Sandbox or Production keys depending on whether you are in Sandbox mode when you create them.

    Here’s a .NET example of a text translation call:

    namespace LCTestApp

    {

        using System;

        using System.Net.Http;

        using System.Net.Http.Headers;

        using System.Runtime.Serialization.Json;

        using System.Text;

        using System.Xml;

        using System.Xml.Linq;

        using System.Xml.XPath;

     

        class Program

        {

            static void Main(string[] args)

            {

                string apiKey = "[Your API Key Here]";

                string sourceText = "Hello Developers";

                string sourceLanguage = "eng";

                string targetLanguage = "fra";

                string json = string.Format(@"{{""text"":""{0}"",""from"":""{1}"",""to"":""{2}""}}", sourceText, sourceLanguage, targetLanguage);

                var client = new HttpClient();

                var requestMessage = new HttpRequestMessage(HttpMethod.Post, "https://lc-api.sdl.com/translate");

                requestMessage.Headers.Add("Accept", "application/json");

                requestMessage.Headers.Add("Authorization", string.Format("LC apiKey={0}", apiKey));

                requestMessage.Content = new ByteArrayContent(Encoding.UTF8.GetBytes(json));

                requestMessage.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json; charset=UTF-8");

                HttpResponseMessage response = client.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead).Result;

                if (response.IsSuccessStatusCode)

                {

                    byte[] responseBytes = response.Content.ReadAsByteArrayAsync().Result;

                    XmlReader reader = JsonReaderWriterFactory.CreateJsonReader(responseBytes, new XmlDictionaryReaderQuotas());

                    XElement root = XElement.Load(reader);

                    XElement translation = root.XPathSelectElement("//translation");

                    Console.WriteLine(translation.Value);

                }

                else

                {

                    Console.WriteLine("#ERROR: " + Encoding.UTF8.GetString(response.Content.ReadAsByteArrayAsync().Result));

                }

     

                Console.ReadKey();

            }

        }

    }

    David Pooley | Senior Product Manager | SDL

Children
No Data