Azure机器学习-文本分析C#错误的请求体,即使在验证JSON体之后也是如此
本文关键字:JSON 验证 之后 文本 错误 Azure 请求 机器学习 | 更新日期: 2023-09-27 17:58:08
使用Azure机器学习-文本分析RESTapi,位于此处。需要通过POST向服务器发送有效负载。我正在尝试获得与IBM watson 类似的结果
以下是我在控制台应用程序中尝试的内容,以下是核心代码:
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);
序列化"文档"的消息体是:
{
"documents": [
{
"language": "en",
"id": "4",
"text": "Lateral internal sphincterotomy and fissurectomy"
},
{
"language": "en",
"id": "5",
"text": "Fissurectomy and Botox injection"
}
]}
我收到错误请求错误。我已经验证了我的请求是否已发送并通过身份验证(之前已失败)。我也尝试过很多变化。
我可以尝试我的请求主体,当将文本从调试变量直接复制到Azure提供的主体时,它可以正常工作:
https://westus.dev.cognitive.microsoft.com/docs/services/TextAnalytics.V2.0/operations/56f30ceeeda5650db055a3c6/console
如果我使用上面的测试,我会得到预期的响应,状态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": []
}
我使用JQuery
和REST API
进行情绪分析。我收到的错误与您收到的错误相同。我设法通过提供输入的JSON-serialized version
作为请求主体使其工作。
这是工作代码-
$(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");
});
});
@Makk,看起来您使用的是C#代码。在文本分析文档中,有一个快速入门部分,其中有一个运行C#的示例,应该对您有用。