0
votes

Working with Azure Machine Learning - Text Analytics REST api, located here. Requires sending a payload to the server via POST. I am trying to get similar results as I do with IBM watson

Here is what I tried in console app, here's core code:

static IRestResponse GetResp(string url, string key, string jsonText) {
    IRestClient client = new RestClient(url);
    IRestRequest request = new RestRequest() { RequestFormat = DataFormat.Json };  
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("Ocp-Apim-Subscription-Key", key);
    IRestResponse response = client.ExecuteAsPost(request, "POST");

}

//  Here the code that serializes the object to look precisely like body advertised calls it: 
string json = JsonConvert.SerializeObject(documents);
IRestResponse resp = GetResponse("https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases", TaxonomyGlueKey, json);

message body from serializing "documents" is:

{
 "documents": [
   {
  "language": "en",
  "id": "4",
  "text": "Lateral internal sphincterotomy and fissurectomy"
  },
  {
  "language": "en",
  "id": "5",
  "text": "Fissurectomy and Botox injection"
  }
]} 

I get Bad Request errors. I've verified my request is sent and passing authentication (it had failed prior). I have tried many variations on this as well.

I am able to try my request body out and it works properly when copying text from debug variable directly to the body provided by Azure:

https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0/operations/56f30ceeeda5650db055a3c6/console

If I test using the above I get the response expected, status 200:

Transfer-Encoding: chunked
x-aml-ta-request-id: c4ea9fff-8068-42a3-99c4-68717acddcf5
X-Content-Type-Options: nosniff
apim-request-id: e5eb593b-96a3-4806-9143-1d83424569be
Date: Thu, 21 Jul 2016 14:14:44 GMT
Content-Type: application/json; charset=utf-8

{
   "documents": [
      {
       "keyPhrases": [
         "fissurectomy"
      ],
      "id": "4"
    },
   {
      "keyPhrases": [
        "Botox injection"
      ],
      "id": "5"
    }
  ],
  "errors": []
}
2

2 Answers

1
votes

I was working with JQuery and REST API for the Sentiment Analysis. I had received the same error as you have received. I managed to get it working by providing the JSON-serialized version of the input as the request body.

Here is the working code-

$(function() {
var params ={
"documents": [
{
"language": "en",
 "id": "1",
 "text": "this is AWESOME!"
}
]
};


$.ajax({
url: "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment?" + $.param( params );,
beforeSend: function(xhrObj){
// Request headers
xhrObj.setRequestHeader("Content-Type","application/json");
xhrObj.setRequestHeader("Ocp-Apim-Subscription-Key","<your subscription key here>");
xhrObj.setRequestHeader("Accept","application/json");
},
type: "POST",
// Request body
data: JSON.stringify(params)
})
.done(function(data) {
        alert("Sentiment score is " + data.documents[0].score);

    })
    .fail(function() {
        alert("error");
    });
});
0
votes

@Makk, it looks like you are using C# code. In the text analytics documentation, there is a quick-start section that has running C# sample that should work for you.