The Bing Spellcheck API only returns empty results.

David Peng 0 Reputation points
2023-12-10T08:02:39.13+00:00

Hi,

I've followed the instructions from the tutorial here Microsoft's Bing Spell Check API Tutorial to set up a project, then I tested it with A apple a day, keeps doctor away. Still, the spell check keeps returning the following empty result: {"_type": "SpellCheck", "flaggedTokens": []}. Have I done something wrong?

Here is my code:

const key = 'MY_KEY';

function getSubscriptionKey() {
  return key;
}

function pre(text: string) {
  return '<pre>' + text.replace(/&/g, '&amp;').replace(/</g, '&lt;') + '</pre>';
}

function renderSearchResults(results: any) {
  document.getElementById('results').innerHTML = pre(JSON.stringify(results, null, 2));
}

function renderErrorMessage(message: string, code?: number) {
  if (code) document.getElementById('results').innerHTML = '<pre>Status ' + code + ': ' + message + '</pre>';
  else document.getElementById('results').innerHTML = '<pre>' + message + '</pre>';
}

function bingSpellCheck(query: string, key: string) {
  const endpoint = 'https://api.bing.microsoft.com/v7.0/spellcheck/';
  // const endpoint = 'https://api.cognitive.microsoft.com/bing/v7.0/spellcheck/';

  const request = new XMLHttpRequest();

  try {
    request.open('GET', endpoint + '?mode=proof&mkt=en-US&text=' + encodeURIComponent(query));
  } catch (e) {
    renderErrorMessage('Bad request');
    return false;
  }

  request.setRequestHeader('Ocp-Apim-Subscription-Key', key);

  request.addEventListener('load', function () {
    if (this.status === 200) {
      renderSearchResults(JSON.parse(this.responseText));
    } else {
      if (this.status === 401) getSubscriptionKey();
      renderErrorMessage(this.statusText, this.status);
    }
  });

  request.addEventListener('error', function () {
    renderErrorMessage('Network error');
  });

  request.addEventListener('abort', function () {
    renderErrorMessage('Request aborted');
  });

  request.send();
  return false;
}

document.querySelector('form').addEventListener('submit', function (e) {
  e.preventDefault();
  bingSpellCheck(document.querySelector('input').value, getSubscriptionKey());
});

Additionally, I've noticed an incorrect API endpoint in the tutorial.

Bing Spell Check
Bing Spell Check
A Bing service that detects and corrects spelling mistakes in your app.
33 questions
{count} votes

1 answer

Sort by: Most helpful
  1. navba-MSFT 19,655 Reputation points Microsoft Employee
    2024-01-03T05:48:18.4033333+00:00

    @David Peng Thanks for getting back. Since the same curl command is working fine at my end. Also it is working fine on postman. I am thinking it might be something on the network. So as mentioned earlier, could you please test it completely from a different machine on different network ?
    .
    .
    Also note: Bing distinguishes between a denial-of-service (DoS) attack and a QPS violation. If Bing suspects a DoS attack, the request succeeds (HTTP status code is 200 OK) but the body of the response is empty.
    .
    So in your curl command also include the below headers and check if that helps.

    -H "X-MSEdge-ClientID: 00B4230B74496E7A13CC2C1475056FF4" -H "X-MSEdge-ClientIP: 11.22.33.44"

    .
    Awaiting your reply.

    0 comments No comments