locked
Spell API: unable to do http request RRS feed

  • Question

  • Hi,

    I'm trying to use the Spell API but I'm unable to do the http request.

    This is my code:

    using (var httpClient = new HttpClient(httpClientHandler))
    {
    httpClient.BaseAddress = new Uri("https://api.cognitive.microsoft.com");
    httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _bingSpellCheckApiKey);
    using (var content = new FormUrlEncodedContent(new[]
    {
    new KeyValuePair<string, string>("Text", text)
    }))
    {
    using (var result = await httpClient.PostAsync("/bing/v5.0/spellcheck/", content))
    {
    string resultContent = await result.Content.ReadAsStringAsync();
    return resultContent;
    }
    }
    }

    But I always get this answer:

    "{\"_type\": \"SpellCheck\", \"flaggedTokens\": []}"

    I don't understand what is different from the test page at https://dev.cognitive.microsoft.com/docs/services/56e73033cf5ff80c2008c679/operations/56e73036cf5ff81048ee6727/console

    Thank you, Damiano

    Monday, October 17, 2016 3:26 AM

All replies

  • What are the values of text you've tried?  The response JSON is what you get when there are no spelling errors.
    Wednesday, October 19, 2016 9:05 PM
    Moderator
  • On first glance your code seems to be ok.

    What is the text that you are passing to the API?

    For reference here is a complete example that works with the code you provided. Only difference is that I am using the sample text since you didn't tell us the text you were using.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Net.Http.Headers;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace BingSpellCheckApi2
    {
        class Program
        {
            static void Main(string[] args)
            {
                var res = TestThis().Result;
            }
    
            public static async Task<string> TestThis()
            {
                const string text = "Bill+Gatas";
                const string _bingSpellCheckApiKey = "your_api_key";
                
                using (var httpClient = new HttpClient())
                {
                    httpClient.BaseAddress = new Uri("https://api.cognitive.microsoft.com");
                    httpClient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", _bingSpellCheckApiKey);
                    using (var content = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair<string, string>("Text", text)
                    }))
                    {
                        using (var result = await httpClient.PostAsync("/bing/v5.0/spellcheck/", content))
                        {
                            string resultContent = await result.Content.ReadAsStringAsync();
                            return resultContent;
                        }
                    }
                }
            }
        }
    }
    

     



    Life is one big road with a lot of signs.

    Wednesday, October 19, 2016 10:07 PM