1
votes

I'm trying to consume Azure REST API to update the Knowledge Base I created via QnA Maker. There's a link there to go to the API testing console.

I'm trying to use the code below to replace the contents of my Knowledge Base with something that I pull from a different data source. See my code below. hopefully it makes sense

function synchronize() {

    var jsonData = {
        "add": {
            "qnaList":[
                {"source": "Custom"},
                {"answer": "Hello"},
                {"questions": ["Hi", "Hello"]}
            ],
        },
        "delete": {
            "sources": ["Custom"]
        },
        "update": {}
    }   

    var request = new XMLHttpRequest();    
    var parameters = {
        "body": jsonData
    }
    request.open("POST", "https://qnawcfaq.azurewebsites.net/qnamaker/knowledgebases/{kbId}}/generateAnswer", true);                    
    request.setRequestHeader("Authorization", "EndpointKey {key}}");
    request.setRequestHeader("Content-type", "application/json");                    
    request.onreadystatechange = function () { //Call a function when the state changes.
        if (request.readyState == 4 && request.status == 200) {
            alert(request.responseText);
        }
    }

    request.send(JSON.stringify(parameters));
}

I am expecting something like the following:

{
  "operationState": "NotStarted",
  "createdTimestamp": "2018-03-19T07:38:46Z",
  "lastActionTimestamp": "2018-03-19T07:39:29Z",
  "userId": "86bb8390-56c0-42c2-9f81-3de161981191",
  "operationId": "03a4f4ce-30a6-4ec6-b436-02bcdf6153e1"
}

However, I get the following error:

{
  "error": {
    "code": "BadArgument",
    "message": "Authorization"
  }
}

The value that I used in the Ocp-Apim-Subscription-Key works against their API Testing Console but it doesn't work with the code above. Any idea what I'm missing here?

Thanks!

Working Solution Uploaded in Github: Solution

1
you uri is wrong and the header "Ocp-Apim-Subscription-Key" toFrV
@FrV, can you provide the correct uri and header? Can you provide an example please? Thanks!ericute

1 Answers

0
votes

Update 1: After reading the question again, questioner wants to create a QnAMaker pair and not just query it. The documentation of QnAMaker API is not really good and in some ways outdated or confusing. Hence I share hints for both scenarios.

Scenario 1: Creating QnAMaker pairs

To create QnAMaker pairs it's a bit more difficult and I would recommend starting with the C# Sample and its Program.cs. From there you can tansform it to java script. The documentation has samples for other languages as well, e.g. for nodejs.

Protip: Run Fiddler along while you execute the samples. This way you can check call by call and port to postman or composer for your tests.

Based on the original question, a correct call goes like this:

// Replace {key} with your QnAMaker endpoint key 
// and {kbId} with the id of the knowledgebase you want to upgrade.

function synchronize() {
    var jsonData = {
        qnaList: [
          {
            id: 0,
            answer: 'Hello',
            source: 'Custom Editorial',
            questions: [
              'Hi','Hello'
            ],
            metadata: [
              {
                name: 'category',
                value: 'api'
              }
            ]
          }
        ]
    }

    var request = new XMLHttpRequest();    

    request.open("PATCH", "https://westus.api.cognitive.microsoft.com/qnamaker/v4.0/knowledgebases/{kbId}", true);                    
    request.setRequestHeader("Ocp-Apim-Subscription-Key", "{key}");
    request.setRequestHeader("Content-type", "application/json");                    
    request.onreadystatechange = function () { 
        //Call a function when the state changes.
        if (request.readyState == 4) {
            alert(request.responseText);
        }
    }

    request.send(JSON.stringify(jsonData));
}

Scenario 2: Query QnAMaker

For POST /generateAnswer, Ocp-Apim-Subscription-Key is obsolete and you have to use your own uri (host) instead of westus.api.cognitive.microsoft.com.

When QnAMaker became GA summer last year, the header and host definition changed.

The easiest way to find the correct settings for all of your parameters is to go to the QnAMaker portal an click View Code on your knowledge base. enter image description here

From there you'll find the correct settings to copy it over. enter image description here

The side by side comparison of the changed definition below was posted on GA announcement.

Announcement